NIO.2 Flashcards

1
Q

Path object

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

3 ways to create a Path object using the Paths class

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Should you use forward slashes (/) or backward slashes () when separating file names in paths?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to convert a Path object path to a URI

A

path.toURI()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to create a Path object using the FileSystems class

A

Path path =FileSystems.getDefault().getPath(string);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to access a remote filesystem

A

FileSystem fs = FileSystems.getFileSystem(uri);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Optional argument

NOFOLLOW_LINKS

A

If provided, symbolic links when encountered will not be traversed. Useful for performing operations on symbolic links themselves rather than their target.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Optional argument

FOLLOW_LINKS

A

If provided, symbolic links when encountered will be traversed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Optional argument

COPY_ATTRIBUTES

A

If provided, all metadata about a file will be copied with it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Optional argument

REPLACE_EXISTING

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Optional argument

ATOMIC_MOVE

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Path methods

getNameCount()

getName(int)

A
  • Returns number of elements in the path
  • Returns the name of the element at the specified position

Note: The root element / is not counted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Path methods

getFileName()

getParent()

getRoot()

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Path methods

isAbsolute()

toAbsolutePath()

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Path method

subpath(int a, int b)

A

Returns a relative subpath of the Path object, referenced by an inclusive start index and an exclusive end index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Path method

path1.relativize(path2)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Path method

path1.resolve(path2)

A

If path2 is relative, then the result is path2 appended to path1

If path2 is absolute, then path2 is returned

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Path method

normalize()

A

Returns a path that is this path with redundant name elements eliminated.

19
Q

Path method

toRealPath()

A

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

20
Q

Files method

Files.exists(Path)

A

Returns true if it references a file that exists in the file system.

21
Q

Files method

Files.isSameFile(path1,path2)

A

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

22
Q

Files methods

Files.createDirectory(Path)

Files.createDirectories(Path)

A
  • 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

23
Q

Files method

Files.copy(path1, path2)

A

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

24
Q

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);
}

A
  • Copies the input stream data to the file wolf.txt

- Copies the data of clown.xsl to the output stream

25
Q

Files method

Files.move(path1, path2)

A

Moves or renames path1 to path2 in the file system. Will not overwrite an existing file by default. Will preserve attributes by default.

throws the checked IOException in the event that the file or directory could not be found or moved.

26
Q

Files methods

Files.delete(path1)

Files.deleteIfExists(path1)

A
  • Deletes the file represented by path1. If path1 is a non-empty directory, then DirectoryNotEmptyException will be thrown. If path1 is a symlink, then the symlink will be deleted, not the file that the symlink points to. Throws NoSuchFileException if path1 doesn’t exist in the file system
  • Same as delete(), but will return false if the file doesn’t exist instead of throwing an exception
27
Q

Files method

Files.newBufferedReader(path1,charset)

A

Returns a BufferedReader to read in the file represented by path1, using the encoding method specified by charset

Note: Charset.defaultCharset() can be used to make things easy

28
Q

Files method

Files.newBufferedWriter(path1, charset)

A

Returns a BufferedWriter to write to the file represented by path1, using the encoding method specified by charset

29
Q

Files method

Files.readAllLines()

A

Reads all of the lines of a text file and returns the results as an ordered List of String values.

Throws an IOException if the file cannot be read

30
Q

Files methods

Files.isDirectory(Path)

Files.isRegularFile(Path)

Files.isSymbolicLink(Path)

A
  • Returns true if is a directory. It will look at what a symbolic link points to if you give it that.
  • Return true if not a directory, resource, or some other janky file. It will look at what a symbolic link points to if you give it that.
  • Returns true if symbolic link
31
Q

Files method

Files.isHidden(path1)

Files.isReadable(path2)

Files.isExecutable(path3)

A
  • Returns true if path1 is a hidden file
  • Returns true if path2 is readable
  • Returns true if path3 is executable
32
Q

Files method

Files.size(Path)

A

Returns the size of the file in bytes as a long value.

33
Q

Files methods

Files.getLastModifiedTime(Path)

Files.setLastModifiedTime(Path,FileTime)

A
  • Returns a FileTime object which contains info on when the file was last modified.
  • Sets the last modified time of the file to the specified info in the FileTime object

Both may throw IOException

Note: FileTime has toMillis() and fromMillis() to express the FileTime object as milliseconds from the epoch

34
Q

Files methods

Files.getOwner(Path)

Files .setOwner(Path,UserPrincipal)

A
  • Returns an instance of UserPrincipal that represents the owner of the file within the file system.
  • Set the owner of the file to the UserPrincipal object

Both may throw IOException

35
Q

View

A

A group of related attributes for a particular file system type

36
Q

Files methods

Files.readAttributes(Path, Class)

Files.getFileAttributeView(Path, Class)

A
  • Returns a read-only view of the file attributes.
  • Returns the underlying attribute view, and it provides a direct resource for modifying file information.

Both may throw IOException

37
Q

BasicFileAttributes

A

The object type that you can store a files attributes in and call methods on to retrieve those attributes

38
Q

BasicFileAttributes methods

isOther()

fileKey()

A
  • Used to check for paths that are not files, directories, or symbolic links, such as paths that refer to resources or devices in some file systems.
  • Returns a file system value that represents a unique identifier for the file within the file system or null if it is not supported by the file system.
39
Q

BasicFileAttributeView

A

The object type used to modify a file’s set of date/time values.

40
Q

BasicFileAttributeView method

setTimes(FileTime lastModifiedTime, FileTime lastAccessTime, FileTime createTime)

A

Used to update the time attributes of the file.

41
Q

Files method

Files.walk(path)

A

Returns a Stream object that traverses the directory in a depth-first, lazy manner.

May throw IOException

42
Q

Files method

Files.find(Path, int, BiPredicate)

A

Returns a Stream that traverses the directory specified, to a max depth of the int specified, filtering the stream on the BiPredicate provided

43
Q

Files method

Files.list(Path)

A

Returns a Stream containing the contents of the directory specified, only to one level, so no traversing down the rest of the structure.

44
Q

Files method

Files.lines(Path)

A

Returns a Stream of all the lines in a file