Reading and writing files

Read project files with java.nio.file.Files; write temporary files under /tmp.

Runs in Zerethon IDE · Java 25 Official Java documentation ↗
Main.java
import java.nio.file.*;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        List<String> lines = Files.readAllLines(Path.of("data.csv"));
        for (String line : lines.subList(1, lines.size())) {
            System.out.println(line.split(",")[0]);
        }

        Path out = Path.of("/tmp/report.txt");
        Files.writeString(out, "rows=" + (lines.size() - 1));
        System.out.println(Files.readString(out));
    }
}
data.csv
name,score
ada,90
alan,85

Output

ada
alan
rows=2
  • Paths are relative to the project root (Path.of("data/input.txt")).
  • File methods throw IOException: declare throws Exception on main, or catch it.
  • Project files are read-only; writing next to them throws FileSystemException … Read-only file system. Write to /tmp.