NIO Flashcards
How can we create directories?
Files.createDirectory(Path.of(“..”))
Files.createDirectories(Path.of(“..”))
How can we get File from Path
path.toFile()
How can we get Path from File
file.toPath()
What should we watch out when using File?
If file we work with is regular file or directory
file.exists()
Checks whether the file or directory denoted by this abstract pathname exists.
How can we check if a file or directory exists?
file.exists()
file.getAbsolutePath()
Returns the absolute pathname string of this abstract pathname.
How can we get the absolute path of a file?
file.getAbsolutePath()
file.isDirectory()
Tests whether the file denoted by this abstract pathname is a directory.
How can we check if a path represents a directory?
file.isDirectory()
file.getParent()
Returns the pathname string of this abstract pathname’s parent, or null if this pathname does not name a parent directory.
How can we get the parent directory of a file?
file.getParent()
file.length()
Returns the length of the file denoted by this abstract pathname
How can we get the size of a file?
file.length()
Files.exists(path)
Tests whether a file or directory exists at the specified path.
How can we check if a file or directory exists using the Files class?
Files.exists(path)
Files.isRegularFile(path)
Tests whether a file is a regular file with opaque content.
How can we check if a path represents a regular file?
Files.isRegularFile(path)
Files.size(path)
Returns the size of a file (in bytes).
How can we get the size of a file using the Files class?
Files.size(path)
Files.list(path)
Returns a lazily populated Stream of entries in the directory
How can we get a stream of entries in a directory?
Files.list(path)
How can we create Paths?
Path.of()
Path.get()
path.getNameCount()
Returns the number of name elements in the path.
How can we get the number of name elements in a path?
path.getNameCount()
path.getName(i)
Returns a name element of this path as a Path object. The index parameter specifies which name element to return, counting from zero (the root component, if present).
How can we get a specific name element from a path?
path.getName(i)
Are paths immutable?
Yes
How can we prove paths immutability?
path.resolve(“dir”). this sets new path that we need to use with variable
What happens if we mix absolute and relative paths?
Absolute paths trumps relative paths
path.getFileName()
Returns the name of the file or directory denoted by this path as a Path object.
How can we get the file name from a path?
path.getFileName()
path.getRoot()
Returns the root component of this path as a Path object, or null if this path does not have a root component.