9. NIO2 Flashcards

1
Q

What does enum NOFOLLOW_LINKS mean?

A

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

Test file existing
Read file data
Copy file
Move file

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

What does enum FOLLOW_LINKS mean?

A

If provided, symbolic links when encountered
will be traversed.

Traverse a directory tree

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

What does enum COPY_ATTRIBUTES mean?

A

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

Copy file

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

What does enum REPLACE_EXISTING mean?

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

Copy file
Move file

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

What does enum ATOMIC_MOVE mean?

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 (AtomicMoveNotSupportedException)

Move file

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

How to get a referance to Path interface?

A

There’s Paths factory that provides platform specific constructors

Path p = Paths.get(String) or Path p = Paths.get(String, String…)

The second method joins the directories with a platform specific path separator.

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

Can URI be constructed from relative path?

A

No, URI always needs an absolute path.

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

Method that “Returns the string representation of this path.”

A

String toString()

Returns the string representation of this path.
If this path was created by converting a path string using the getPath method then the path string returned by this method may differ from the original String used to create the path.

The returned path string uses the default name separator to separate names in the path.

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

Method that “Returns the number of name elements in the path.”

A

int getNameCount()

Returns the number of name elements in the path.

Returns:
the number of elements in the path, or 0 if this path only represents a root component

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

Method that “Returns a name element of this path as a Path object.”

A

Path getName(int index)

Returns a name element of this path as a Path object.
The index parameter is the index of the name element to return. The element that is closest to the root in the directory hierarchy has index 0. The element that is farthest from the root has index count-1.

Parameters:
index - the index of the element
Returns:
the name element

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

What is the getNameCount() of path representing root?

A

0

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

Method that “Returns the name of the file or directory denoted by this path as a Path object.”

A

Path getFileName()

Returns the name of the file or directory denoted by this path as a Path object. The file name is the farthest element from the root in the directory hierarchy.

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

Method that “Returns the parent path, or null if this path does not have a parent.”

A

Path getParent()

Returns the parent path, or null if this path does not have a parent.
The parent of this path object consists of this path’s root component, if any, and each element in the path except for the farthest from the root in the directory hierarchy. This method does not access the file system; the path or its parent may not exist. Furthermore, this method does not eliminate special names such as “.” and “..” that may be used in some implementations. On UNIX for example, the parent of “/a/b/c” is “/a/b”, and the parent of “x/y/.” is “x/y”. This method may be used with the normalize method, to eliminate redundant names, for cases where shell-like navigation is required.

If this path has one or more elements, and no root component, then this method is equivalent to evaluating the expression:

subpath(0, getNameCount()-1);

Returns:
a path representing the path’s parent

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

Method that “Returns the root component of this path as a Path object, or null if this path does not have a root component.”

A

Path getRoot()

Returns the root component of this path as a Path object, or null if this path does not have a root component.

If path is relative, it returns null.

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

Method that “Tells whether or not this path is absolute.”

A

boolean isAbsolute()

Tells whether or not this path is absolute.
An absolute path is complete in that it doesn’t need to be combined with other path information in order to locate a file.

Returns:
true if, and only if, this path is absolute

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

Method that “Returns a Path object representing the absolute path of this path.”

A

Path toAbsolutePath()

Returns a Path object representing the absolute path of this path.
If this path is already absolute then this method simply returns this path. Otherwise, this method resolves the path in an implementation dependent manner, typically by resolving the path against a file system default directory. Depending on the implementation, this method may throw an I/O error if the file system is not accessible.

Returns:
a Path object representing the absolute path

17
Q

What will be returned by path.subpath(1,1)?

A

IllegalArgumentException - if beginIndex is negative, or greater than or equal to the number of elements. If endIndex is less than or equal to beginIndex, or larger than the number of elements.

18
Q

Method that “Returns a relative Path that is a subsequence of the name elements of this path.”

A

Path subpath(int beginIndex, int endIndex)

Returns a relative Path that is a subsequence of the name elements of this path.
The beginIndex and endIndex parameters specify the subsequence of name elements. The name that is closest to the root in the directory hierarchy has index 0. The name that is farthest from the root has index count-1. The returned Path object has the name elements that begin at beginIndex and extend to the element at index endIndex-1.

Parameters:
beginIndex - the index of the first element, inclusive
endIndex - the index of the last element, exclusive
Returns:
a new Path object that is a subsequence of the name elements in this Path

19
Q

Method that “Constructs a relative path between this path and a given path.”

A

Path relativize(Path other)

Constructs a relative path between this path and a given path.
Relativization is the inverse of resolution. This method attempts to construct a relative path that when resolved against this path, yields a path that locates the same file as the given path. For example, on UNIX, if this path is “/a/b” and the given path is “/a/b/c/d” then the resulting relative path would be “c/d”. Where this path and the given path do not have a root component, then a relative path can be constructed. A relative path cannot be constructed if only one of the paths have a root component. Where both paths have a root component then it is implementation dependent if a relative path can be constructed. If this path and the given path are equal then an empty path is returned.

For any two normalized paths p and q, where q does not have a root component,

p.relativize(p.resolve(q)).equals(q)
When symbolic links are supported, then whether the resulting path, when resolved against this path, yields a path that can be used to locate the same file as other is implementation dependent. For example, if this path is “/a/b” and the given path is “/a/x” then the resulting relative path may be “../x”. If “b” is a symbolic link then is implementation dependent if “a/b/../x” would locate the same file as “/a/x”.

Parameters:
other - the path to relativize against this path
Returns:
the resulting relative path, or an empty path if both paths are equal

The relativize() method requires that both paths be absolute or both relative, and
it will throw an IllegalArgumentException if a relative path value is mixed with an
absolute path value.

20
Q

Method that “Resolve the given path against this path.”

A

Path resolve(Path other)

Resolve the given path against this path.
If the other parameter is an absolute path then this method trivially returns other. If other is an empty path then this method trivially returns this path. Otherwise this method considers this path to be a directory and resolves the given path against this path. In the simplest case, the given path does not have a root component, in which case this method joins the given path to this path and returns a resulting path that ends with the given path. Where the given path has a root component then resolution is highly implementation dependent and therefore unspecified.

Parameters:
other - the path to resolve against this path

Returns:
the resulting path

21
Q

Method that “Converts a given path string to a Path and resolves it against this Path in exactly the manner specified by the resolve method.”

A

Path resolve(String other)

Converts a given path string to a Path and resolves it against this Path in exactly the manner specified by the resolve method. For example, suppose that the name separator is “/” and a path represents “foo/bar”, then invoking this method with the path string “gus” will result in the Path “foo/bar/gus”.

Parameters:
other - the path string to resolve against this path
Returns:
the resulting path

22
Q

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

A

Path normalize()

Returns a path that is this path with redundant name elements eliminated.
The precise definition of this method is implementation dependent but in general it derives from this path, a path that does not contain redundant name elements. In many file systems, the “.” and “..” are special names used to indicate the current directory and parent directory. In such file systems all occurrences of “.” are considered redundant. If a “..” is preceded by a non-“..” name then both names are considered redundant (the process to identify such names is repeated until it is no longer applicable).

This method does not access the file system; the path may not locate a file that exists. Eliminating “..” and a preceding name from a path may result in the path that locates a different file than the original path. This can arise when the preceding name is a symbolic link.

Returns:
the resulting path or this path if it does not contain redundant name elements; an empty path is returned if this path does have a root component and all name elements are redundant