![]() ![]() Path path2_to_path1 = path2.relativize(path1) Path path1_to_path2 = path1.relativize(path2) Path path2 = Paths.get("C:\\Users\\Java\\examples") This can be used to navigate between two paths. In NIO, when we want to construct a Path from one location to another we can use the method relativize. Output is C:\Users\Java\examples\Test.java Path path1 = Paths.get("C:\\Users\\Java\\examples") If an absolute path is passed as parameter to resolve method, then the same is returned. To join two paths in NIO, we can use the resolve method. ![]() Path currentDirectory = Paths.get("").toAbsolutePath() To get the current working directory with NIO, we can use the following code and it returns absolute path of current working directory. So the same code can be written as FileSystems.getDefault().getPath("/etc/user/foo") Get Current Path NOTE: Paths.get is a convenience method for FileSystems.getDefault().getPath. Path filePath = Paths.get("/etc/user/foo") Path path = FileSystems.getDefault().getPath("lib", "nio.jar") įollowing is another example to instantiate a file (absolute) path. How to create a file Path?įollowing is an example way to instantiate a file (relative) path, “lib” is a directory name relative to current directory and “nio.jar” is a file name in that directory. is an interface and it is used to represent a system dependent file path which can be used to locate a file in a file system. In case of relative path, we need to combine it with another path to get the absolute path to locate the file. ![]() So the absolute path starts with the root node and ends with the file name identifier. Absolute path is self sufficient, that is a file can be located with an absolute path without any other information. There are two types of paths, relative and absolute. There can be multiple root nodes (C:, D:) as we have in Windows.Ī path can be used to identify and locate a file in the file system. Tree structure starts with a root node and branches. Tree structure based file system is popularly prevalent in current day operating systems. Files are stored in a disk or some media based on a file system. Path is step one in files and IO processing. In this tutorial, we will see about what is a Path and a File with respect to Java NIO. This tutorial is part of the Java NIO tutorial series. ![]()
0 Comments
Leave a Reply. |