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);
How to use Files static method to copy files into a directory?
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
How to use Files static method to Move or Rename file or directory?
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”));
How to use Files static method to perform an Atomic Move of file or directory?
Performing an Atomic Move
Files.move(Path.of(“mouse.txt”), Path.of(“gerbil.txt”),
StandardCopyOption.ATOMIC_MOVE);
How to use Files static method to delete files or directories?
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”));
How to use Files static method to read data from file?
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);
}
How to use Files static method to write data to file?
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();
}
}
How to use Files static method to read all lines from a text file to a List of String?
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>
What are the Enum StandardOpenOption options available for opening file?
Enum java.nio.file.StandardOpenOption:
What are the common mistakes in using StandardOpenOption properly with Files static methods?