NIO.2 Flashcards
Path object
Represents a hierarchical path on the storage system to a file or directory. In this manner, Path is a direct replacement for the legacy java.io.File class, and conceptually it contains many of the same properties.
Contains support for symbolic links
3 ways to create a Path object using the Paths class
1) Path path1 = Paths.get(file)
2) You could also use the multiple argument version which will insert the proper separation character between each string argument
Path path2 = Paths.get(“mydir1”, “mydir2”, “myfile”) //Same as “mydir1/mydir2/myfile” in linux based
3) You could also use a URI:
Path path3 = Paths.get(new URI(“file:///home/mydir”));
Note that the path used in the URI must be an absolute path.
Should you use forward slashes (/) or backward slashes () when separating file names in paths?
The JVM can convert either to the correct version for the current operating system, so you can use either of them.
Note: If you use the \ as the first character in a Linux based file system, it won’t be considered an absolute path still.
Also could throw a URISyntaxException
How to convert a Path object path to a URI
path.toURI()
How to create a Path object using the FileSystems class
Path path =FileSystems.getDefault().getPath(string);
How to access a remote filesystem
FileSystem fs = FileSystems.getFileSystem(uri);
Optional argument
NOFOLLOW_LINKS
If provided, symbolic links when encountered will not be traversed. Useful for performing operations on symbolic links themselves rather than their target.
Optional argument
FOLLOW_LINKS
If provided, symbolic links when encountered will be traversed.
Optional argument
COPY_ATTRIBUTES
If provided, all metadata about a file will be copied with it.
Optional argument
REPLACE_EXISTING
If provided and the target file exists, it will be replaced; otherwise, if it is not provided, an exception will be thrown if the file already exists.
Optional argument
ATOMIC_MOVE
The operation is performed in an atomic manner within the file system, ensuring that any process using the file sees only a complete record. Method using it may throw an exception if the feature is unsupported by the file system.
Path methods
getNameCount()
getName(int)
- Returns number of elements in the path
- Returns the name of the element at the specified position
Note: The root element / is not counted
Path methods
getFileName()
getParent()
getRoot()
- Returns a Path instance representing the filename, which is the farthest element from the root.
- Returns a Path instance representing the parent path or null if there is no such parent. It will only go as far up the path as is explicitly specified in the path definition.
- Returns the root element for the Path object or null if the Path object is relative.
Path methods
isAbsolute()
toAbsolutePath()
- Returns true if the path the object references is absolute and false if the path the object references is relative.
- Converts a relative Path object to an absolute Path by joining it to the current working directory.
Path method
subpath(int a, int b)
Returns a relative subpath of the Path object, referenced by an inclusive start index and an exclusive end index.
Path method
path1.relativize(path2)
returns the Path from path1 to path2
both must be relative or both must be absolute, and an IllegalArgumentException will be thrown if absolute and relative paths are mixed
Note: these do not necessarily have to be actual files in the filesystem, since it just looks at the two Paths it gives you
Path method
path1.resolve(path2)
If path2 is relative, then the result is path2 appended to path1
If path2 is absolute, then path2 is returned