// Created on savesnippets.com ยท https://savesnippets.com/e8VLXJBAM7fuL0 import java.io.IOException; import java.nio.file.*; import java.nio.charset.StandardCharsets; import java.util.List; class Demo { void example() throws IOException { var path = Path.of("config.toml"); // Whole file โ†’ String (Java 11+) String contents = Files.readString(path); System.out.println("len = " + contents.length()); // Whole file โ†’ List (one element per line) List lines = Files.readAllLines(path); for (var line : lines) System.out.println(line); // Specify charset explicitly when unsure Files.readString(path, StandardCharsets.UTF_8); // Stream lines lazily โ€” constant memory, good for big files try (var stream = Files.lines(path)) { stream.filter(l -> !l.startsWith("#")) .forEach(System.out::println); } } }