Read and Write Text Files
The Files class offers a couple of convenience methods
to read and write text content from/to files. When converting between Unicode
strings and binary files content an encoding is applied. Do not rely on
system defaults, always supply an encoding for portable behavior.
Since Java 11
import java.io.IOException;
import java.io.LineNumberReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
public class ReadWriteTextFiles {
	static final String TEST_CONTENT = "Line 1\nLine 2\nLine 3\n";
	void main() throws IOException {
		var file = Files.createTempFile("ReadWriteTextFiles", ".txt");
		// Write out a string to a file
		Files.writeString(file, TEST_CONTENT, StandardCharsets.UTF_8);
		// Read the entire content as one string:
		IO.print(Files.readString(file, StandardCharsets.UTF_8));
		// Read the entire content as a stream of lines:
		Files.lines(file, StandardCharsets.UTF_8).map("> "::concat).forEach(IO::println);
		// Read the entire content into a list of lines:
		var lines = Files.readAllLines(file, StandardCharsets.UTF_8);
		IO.println(String.join("/", lines));
		// If we need a Reader instance we can directly open a file as a BufferedReader
		try (var reader = new LineNumberReader(Files.newBufferedReader(file, StandardCharsets.UTF_8))) {
			String line;
			while ((line = reader.readLine()) != null) {
				IO.println("%d: %s".formatted(reader.getLineNumber(), line));
			}
		}
		Files.delete(file);
	}
}
This snippet at GitHub