Week 53 — What is `java.nio.file.Files` and how does it simplify I/O?

Question of the Week #53
What is java.nio.file.Files and how does it simplify I/O?
4 Replies
Eric McIntyre
Eric McIntyre12mo ago
The Files class provides many convenience methods for accessing files. For example, it's possible to copy or move files:
Files.copy(Path.of("source.txt"),Path.of("target.txt"));//copies source.txt to target.txt
Files.move(Path.of("source.txt"),Path.of("target2.txt"));//moves source.txt to target2.txt
Files.delete(Path.of("target2.txt"));//deletes the target2.txt file
Files.copy(Path.of("source.txt"),Path.of("target.txt"));//copies source.txt to target.txt
Files.move(Path.of("source.txt"),Path.of("target2.txt"));//moves source.txt to target2.txt
Files.delete(Path.of("target2.txt"));//deletes the target2.txt file
Aside from dealing with existing files, it is also possible to create new files and directories:
Files.createFile(Path.of("someFile.txt"));
Files.createDirectory(Path.of("someDirectory"));
Files.createDirectories(Path.of("parentDirectory/childDirectory"));//also creates parent directories if needed
Files.createFile(Path.of("someFile.txt"));
Files.createDirectory(Path.of("someDirectory"));
Files.createDirectories(Path.of("parentDirectory/childDirectory"));//also creates parent directories if needed
Creating temporary directories and files at some platform-specific location and getting their path is also possible. The names of these directories/files is randomized but it is possible to set a prefix (and suffix for files).
Path tempDirectory = Files.createTempDirectory("myapplication-");//creates a temporary directory starting with myapplication- somewhere
Path tempFile = Files.createTempFile("myapplication-",".txt");//creates a temporary file starting with myapplication- and ending with .txt somewhere
Path tempDirectory = Files.createTempDirectory("myapplication-");//creates a temporary directory starting with myapplication- somewhere
Path tempFile = Files.createTempFile("myapplication-",".txt");//creates a temporary file starting with myapplication- and ending with .txt somewhere
Eric McIntyre
Eric McIntyre12mo ago
There are also methods for reading from/writing to files:
Files.write(Path.of("someFile.raw"), new byte[]{13, 37});
Files.write(Path.of("someFile.txt"), List.of("Hello", "World","","This is written into someFile.txt"), StandardCharsets.UTF_8);

byte[] contentAsByteArray = Files.readAllBytes(Path.of("someFIle.raw"));
String contentAsString = Files.readString(Path.of("someFile.txt"), StandardCharsets.UTF_8);
List<String> contentAsStringListSplitByLines = Files.readAllLines(Path.of("someFile.txt"), StandardCharsets.UTF_8);
Files.write(Path.of("someFile.raw"), new byte[]{13, 37});
Files.write(Path.of("someFile.txt"), List.of("Hello", "World","","This is written into someFile.txt"), StandardCharsets.UTF_8);

byte[] contentAsByteArray = Files.readAllBytes(Path.of("someFIle.raw"));
String contentAsString = Files.readString(Path.of("someFile.txt"), StandardCharsets.UTF_8);
List<String> contentAsStringListSplitByLines = Files.readAllLines(Path.of("someFile.txt"), StandardCharsets.UTF_8);
It is also possible to use a Stream for iterating over the lines of a file:
try(Stream<String> lines = Files.lines(Path.of("someFile.txt"), StandardCharsets.UTF_8)){
List<String> linesStartingWithLetter = lines
.filter(line -> !line.isEmpty())
.filter(line -> Character.isLetter(line.charAt(0)))
.toList();
System.out.println(linesStartingWithLetter);
}
try(Stream<String> lines = Files.lines(Path.of("someFile.txt"), StandardCharsets.UTF_8)){
List<String> linesStartingWithLetter = lines
.filter(line -> !line.isEmpty())
.filter(line -> Character.isLetter(line.charAt(0)))
.toList();
System.out.println(linesStartingWithLetter);
}
📖 Sample answer from dan1st
Eric McIntyre
Eric McIntyre12mo ago
With java.nio.file.Files, you can achieve file manipulation tasks with concise code and handle I/O operations efficiently, often reducing the need for boilerplate code when dealing with files and directories.
Submission from ig.imanish
Eric McIntyre
Eric McIntyre12mo ago
It is used for networking and file handling API it is an alternative to IO, it simplifies as it's easier to work with buffers and also can do non blocking io.
Submission from PNJ3.0#1519
Want results from more Discord servers?
Add your server