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
Path method
normalize()
Returns a path that is this path with redundant name elements eliminated.
Path method
toRealPath()
Returns the path as an absolute path
Works with symlinks, which is why it’s different from toAbsolutePath()
Will throw IOException if the path does not exist as an actual file
Files method
Files.exists(Path)
Returns true if it references a file that exists in the file system.
Files method
Files.isSameFile(path1,path2)
Determines if path1 and path2 point to the same file in the filesystem.
Will follow symlinks if necessary
Throws an IOException if either of the files doesn’t exist
Files methods
Files.createDirectory(Path)
Files.createDirectories(Path)
- Creates a directory
- Creates a directory and any nonexistent parent directories specified.
Both can throw IOException if the directory cannot be created or already exists
Files method
Files.copy(path1, path2)
Creates a copy of path1 called path2
For directories it does not copy over the contents to the new place
By default it won’t overwrite any existing files
Throws an IOException if path1 cannot be read or doesn’t exist
What does this code do
try (InputStream is = new FileInputStream(“source-data.txt”);
OutputStream out = new FileOutputStream(“output-data.txt”)) {
Files.copy(is, Paths.get(“c:\mammals\wolf.txt”));
Files.copy(Paths.get(“c:\fish\clown.xsl”), out);
}
- Copies the input stream data to the file wolf.txt
- Copies the data of clown.xsl to the output stream