Files with java.nio
Files and Path read and write whole files in one line, list directories and walk trees. Most methods throw IOException.
Example
import java.nio.file.*;
import java.util.*;
Path p = Files.createTempFile("demo", ".txt");
Files.writeString(p, "one\ntwo\n");
Files.writeString(p, "three\n", StandardOpenOption.APPEND);
List<String> lines = Files.readAllLines(p);
System.out.println(lines + " " + Files.size(p));
Files.delete(p);Result
[one, two, three] 14
Exercise
Read all lines.
Files.(path)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.