Ch20 NIO.2 Flashcards
What are the key classes in java.nio package?
What are these Path methods?
* normalize()
* resolve()
What are these Path methods?
* resolveSibling()
* relativize()
* startsWith()
What are these Path methods?
* endsWith()
* compareTo()
* toRealPath()
How to use Files.getAttribute(Path p, String attribute, LinkOption… options) method to get the last access timestamp of a file?
What are the key FileAttributeView classes?
What are the key methods of File class?
How to make use of File methods in codes?
Use Files static methods to check for existence of a file
var b1 = Files.exists(Paths.get(“/ostrich/feathers.png”));
System.out.println(“Path “ + (b1 ? “Exists” : “Missing”));
Use Files static methods to test uniqueness of a file
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”)));
What Files static methods can be used to create file directory?
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”));
What Files static methods can be used to copy files?
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”));
What Paths static method can be used to create a Path object?
Path filePathObj = Paths.get(“/panda/bamboo.txt”);
How ot use Files static method to copy and replace existing file?
Files.copy(Paths.get(“book.txt”), Paths.get(“movie.txt”),
StandardCopyOption.REPLACE_EXISTING);
How to use Files static method to copy data between file and I/O stream?
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);