+91 81900 98289

Java Read Files

Files.readString reads a whole text file into one String. Files.readAllLines returns a list of lines without the line breaks. Use the list when you want to handle one line at a time.

Both calls throw if the file is missing. Check Files.exists first when a missing file is a normal case, such as a first run with no saved notes.

Example

The loop prints each line. A file version looks the same once the list has been read from disk.

import java.util.List; public class ReadNote { public static void main(String[] args) { List<String> lines = List.of("Java", "Chennai"); for (String line : lines) { System.out.println(line); } } }
The sample uses a list so the page can show the loop without a file on the server. On your machine, replace List.of with Files.readAllLines(Path.of("note.txt")).