Reading & Writing Text Files in Java

The following code snippets demonstrate how to read and write text files in Java.

Reading from Text File

var fileName = Path.of("myfile.txt");
var content = Files.readString(fileName);

Writing to Text File

var fileName = Path.of("myfile.txt");
var content  = "this is a test";
Files.writeString(fileName, content);