Java fila NIO.2 Flashcards

1
Q

How do you get an attribute of a file?

A

Object object = Files.getAttribute(path, “creationTime”,

LinkOption.NOFOLLOW_LINKS);

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

What is the difference between File and Files?

A

File is an old class (java 4) that represents file/directory path names, whereas Files was introduced in java 7 as a utility class with comprehensive support for IO APIs

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

What is the difference between Path and Paths?

A

The path interface represents a file/directory path and defines a useful list of methods. Paths is a utility class that offers only two methods ( both to get the path object)

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

What is the Files.lines() used for?

A

Read contents of a file

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

What is a Path?

A

Programming abstraction to represent the path of a file/directory

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

How do you get the instance of a path?

A

using the get() method of the Paths class

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

What does the Files.list(Path path) method returns?

A

Stream{Path}

It is like a walk() method with dept limit to 1, it is analogous to File.listFiles()

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

What does the Files.walk(Path path) method return?

A

Stream{Path}. It will not traverse symbolic links by default

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

What is the difference between Files.list() and Files.walk()?

A

.list() does not recursively traverse the directories while .walk() does

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

What does the Files.lines() returns?

A

Stream{Lines}

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

Given:

File file = new File(“x”);

how can you get a instance of Path?

A

file.toPath();

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

What are the common optional arguments in NIO2 and its usages?

A

EnumValue:Usage

  • NOFOLLOW_LINKS : test file exists, read file data, copy, move file
  • FOLLOW_LINKS : transverse a directory tree
  • COPY_ATTRIBUTES : copy file
  • REPLACE_EXISTING: copy file/ move file
  • ATOMIC_MOVE: move file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the toAbsolutePath() does?

A

Returns the absolute path of this path. If the path is already absolute, then it just returns it

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

What does the relativize(String other) method do?

A

constructs a relative path between the path and the given path.

  • It requires that both paths be absolute or both relative, or else it will throw a new IllegalArgumentException
  • If this path and the given path are equal then an empty path is returned
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the resolve(Path path)

resolve(String string) method do?

A
  • adds two paths
  • if the other is a absolute path, just returns other
  • if the other path is empty, just return THIS path
  • where the given path has a root component then resolution is highly implementation dependent and therefore unspecified.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

True or false?

Files belong to java.io while File belong to NIO2?

A

False,

File belongs to legacy java.io while Files belong to the NIO2

17
Q

What does the Files.move(Path path, Path path) do?

A

moves or renames a file, throws IOException

18
Q

What does the Files.delete(Path path) do?

A

deles a file or EMPTY directory within the file system. It throws IOException under a variety of circumstances
(if it is a non empty directory)

19
Q

What does the Files.readAllLines(Path path, ?Charset) return?

A

reads the lines of a text file and returns the results as an ordered List of String. Throws IOException, can pass an option charset param

20
Q

Regarding to file attributes…

what is a view?

A

A view is a group of related attributes for a particular file system type

21
Q

What does the Files.readAttributes(Path path, Class{a}, LinkOption options) returns?

A

returns a read only view of the file attributes. May throw Exception

22
Q

What does the

Files.getFileAttributeView(Path path,Class type, LinkOption… options) return?

A

return the underlying attribute view and it provides a direct resource for modifying file information. May throw Exception

23
Q

What are the attributes and view classes?

A

Attributes class: View class

BasicFileAttributes: BasicFileAttributeView

DosFileAttributes:DosFileAttributeview

PosixFileAttributes: PosixFileAttributeView

24
Q

What does the Files.find(Path, int, Bipredicate) does?

A

behaves in a similar maner as the Files.walk(). Returns a Stream{Path}

25
Q

Given the path:

/zoo/animals/bear/koala/food.text

what does

a) path.subPath(1,3)
b) path.subPath(1,2)
c) path.subPath(0,1)

returns?

A

a) animals/bear
b) animals
c) zoo

26
Q

What are some key points regarding Path.getName(int index)

A
  1. Indices for path name start from 0
  2. root (ie: c:) is not included in the path names
  3. \ is not a part of a pathName
  4. if you pass a negative index or a value greater than or equal to the number of elements, or this path has zero name elements, java will throw java.lang.IllegalException