Ch20 NIO.2 Flashcards

1
Q

What are the key classes in java.nio package?

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

What are these Path methods?
* normalize()
* resolve()

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

What are these Path methods?
* resolveSibling()
* relativize()
* startsWith()

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

What are these Path methods?
* endsWith()
* compareTo()
* toRealPath()

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

How to use Files.getAttribute(Path p, String attribute, LinkOption… options) method to get the last access timestamp of a file?

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

What are the key FileAttributeView classes?

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

What are the key methods of File class?

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

How to make use of File methods in codes?

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

Use Files static methods to check for existence of a file

A

var b1 = Files.exists(Paths.get(“/ostrich/feathers.png”));
System.out.println(“Path “ + (b1 ? “Exists” : “Missing”));

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

Use Files static methods to test uniqueness of a file

A

Testing Uniqueness with isSameFile()

public static boolean isSameFile​(Path path, Path path2)
throws IOException

System.out.println(Files.isSameFile(
Path.of(“/animals/monkey/ears.png”),
Path.of(“/animals/wolf/ears.png”)));

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

What Files static methods can be used to create file directory?

A

Making Directories with createDirectory() and createDirectories()

public static Path createDirectory​(Path dir,
FileAttribute<?>… attrs) throws IOException

public static Path createDirectories​(Path dir,
FileAttribute<?>… attrs) throws IOException

Files.createDirectory(Path.of(“/bison/field”));
Files.createDirectories(Path.of(“/bison/field/pasture/green”));

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

What Files static methods can be used to copy files?

A

Copying Files with copy()

public static Path copy(Path source, Path target,
CopyOption… options) throws IOException

Files.copy(Paths.get(“/panda/bamboo.txt”),
Paths.get(“/panda-save/bamboo.txt”));

Files.copy(Paths.get(“/turtle”), Paths.get(“/turtleCopy”));

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

What Paths static method can be used to create a Path object?

A

Path filePathObj = Paths.get(“/panda/bamboo.txt”);

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

How ot use Files static method to copy and replace existing file?

A

Files.copy(Paths.get(“book.txt”), Paths.get(“movie.txt”),
StandardCopyOption.REPLACE_EXISTING);

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

How to use Files static method to copy data between file and I/O stream?

A

Copying Files with I/O Streams

public static long copy(InputStream in, Path target,
CopyOption… options) throws IOException

public static long copy​(Path source, OutputStream out)
throws IOException

try (var is = new FileInputStream(“source-data.txt”)) {
// Write stream data to a file
Files.copy(is, Paths.get(“/mammals/wolf.txt”));
}

Files.copy(Paths.get(“/fish/clown.xsl”), System.out);

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

How to use Files static method to copy files into a directory?

A

Copying Files into a Directory, need to provide a target file name

var file2 = Paths.get(“test.txt”);
System.out.println(“= test path Uri:”+file2.toUri());
// = test path Uri:file:///Users/john.pun/IdeaProjects/java/test.txt
var directory = Paths.get(“enclosure/test3.txt”);
// import static java.nio.file.StandardCopyOption.*;
Files.copy(file2, directory, REPLACE_EXISTING);

var fileResult = Paths.get(“enclosure”).resolve(“test3.txt”);
System.out.println(“= test fileResult path Uri:”+fileResult.toUri());
// = test fileResult path Uri:file:///Users/john.pun/IdeaProjects/java/enclosure/test333.txt

17
Q

How to use Files static method to Move or Rename file or directory?

A

Moving or Renaming Paths with move()

public static Path move​(Path source, Path target,
CopyOption… options) throws IOException

Files.move(Path.of(“c:\zoo”), Path.of(“c:\zoo-new”));

Files.move(Path.of(“c:\user\addresses.txt”),
Path.of(“c:\zoo-new\addresses2.txt”));

18
Q

How to use Files static method to perform an Atomic Move of file or directory?

A

Performing an Atomic Move

Files.move(Path.of(“mouse.txt”), Path.of(“gerbil.txt”),
StandardCopyOption.ATOMIC_MOVE);

19
Q

How to use Files static method to delete files or directories?

A

Deleting a File with delete() and deleteIfExists()

public static void delete(Path path) throws IOException

public static boolean deleteIfExists(Path path) throws IOException

Files.delete(Paths.get(“/vulture/feathers.txt”));
Files.deleteIfExists(Paths.get(“/pigeon”));

20
Q

How to use Files static method to read data from file?

A

Reading Data with newBufferedReader()

public static BufferedReader newBufferedReader(Path path)
throws IOException

var path = Path.of(“/animals/gopher.txt”);
try (var reader = Files.newBufferedReader(path)) {
String currentLine = null;
while((currentLine = reader.readLine()) != null)
System.out.println(currentLine);
}

21
Q

How to use Files static method to write data to file?

A

Writing Data with newBufferedReader()

var list = new ArrayList<String>();
list.add("Smokey");
list.add("Yogi");</String>

var path = Path.of(“/animals/bear.txt”);
try (var writer = Files.newBufferedWriter(path)) {
for(var line : list) {
writer.write(line);
writer.newLine();
}
}

22
Q

How to use Files static method to read all lines from a text file to a List of String?

A

Reading a File with readAllLines()

public static List<String> readAllLines(Path path) throws IOException</String>

var path = Path.of(“/animals/gopher.txt”);
final List<String> lines = Files.readAllLines(path);
lines.forEach(System.out::println);</String>

23
Q

What are the Enum StandardOpenOption options available for opening file?

A

Enum java.nio.file.StandardOpenOption:

24
Q

What are the common mistakes in using StandardOpenOption properly with Files static methods?

A
25
Q

What are the rules of the Files createFile method?

A
26
Q

What are the rules of the Files delete method?

A
27
Q

What are the rules of the Files createDirectory and createDirectories methods?

A
28
Q

How to use a File object to determine whether the path it references exists?

A

var zooFile1=new File(“/home/tiger/data/stripes.txt”);
Systemout.println(zooFile1.exists()); // true if the file exists

29
Q

What is the Files method to search for files?
* Stream<Path> is returned, and is lazily populated
* Must be used within a try-with-resource statement
* It throws IOException
* includes an optional varargs parameter, with options of type FileVisitOption as the last argument (FOLLOW_LINKS)
* maxDepth of 1 means the search for top-level or root folder only, ignore all its subfolders; If we want to search for all folder levels, put Integer.MAX_VALUE.</Path>

A
30
Q

What is the Files method to list file items in the directory?
* Stream<Path> is returned, and is lazily populated
* Must be used within a try-with-resource statement
* It throws IOException</Path>

A
31
Q

What is the Files method to Walk a directory tree?
* Stream<Path> is returned, and is lazily populated
* Must be used within a try-with-resource statement
* It throws IOException
* includes an optional varargs parameter, with options of type FileVisitOption as the last argument (FOLLOW_LINKS)
* maxDepth of 1 means the search for top-level or root folder only, ignore all its subfolders; If we want to search for all folder levels, put Integer.MAX_VALUE.</Path>

A
32
Q

What are the common file attriburtes retrieved by Files.readAttributes()?
Example of codes
try {
BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
FileTime fileTime = attr.creationTime();
} catch (IOException ex) {
// handle exception
}

A
33
Q

What are the rules of FILES static method copy(Path source, Path target, CopyOption… options)?

A
34
Q

What are the rules of FILES static method copy(Path source, OutputStream out)?

A
35
Q

What are the rules of FILES static method copy(InputStream in, Path target, CopyOption… options)?

A
36
Q

What are the CopyOptions available when the target is a Path?

A
37
Q

What are the Paths class static utility functions to get path?

A
38
Q

How to check if two Path objects refer to the same file?

A
  1. If you need the fastest way and know that there are no reparse points and both paths are absolute, it should be safe to use Path.normalize for both paths. You should use File.getAbsolutePath before normalizing if paths may be relative because normalization doesn’t handle it.
  2. If you have to be absolutely sure that both paths refer to the same file, use Files.isSameFile.
  3. You can also use Path.toRealPath method for resolving any reparse points in path so applying it on both paths should give almost same result as Files.isSameFile but in a different way (maybe slower or faster, also it will not match hardlinks while Files.isSameFile will).