Java File HandlingLesson 52Free preview
Java Read Files
This lesson is part of Java · All levels
Summary
Reading files can be done with Files.readAllLines() which returns a row list, or Files.readString() which returns the entire content as a single string. For large files that are not loaded in memory, use BufferedReader to read line by line. All of these methods are in java.nio.file.Files so the code is short.
Key points
- Files.readAllLines(path) produces a List<String>, one element per line
- Files.readString(path) generates the entire file content as one string
- Files.readAllBytes(path) used for binary data
- BufferedReader.readLine() returns null when it reaches the end of the file
- Charsets can be specified, for example, to be consistent
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.