Java:
Reading a text file
How to:
Reading a file is a breeze in Java, especially with java.nio.file
. Here’s a quick example:
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.IOException;
import java.util.stream.Stream;
public class FileReadExample {
public static void main(String[] args) {
Path filePath = Path.of("example.txt");
try (Stream<String> lines = Files.lines(filePath)) {
lines.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Running this with example.txt
containing “Hello, file readers!” would output:
Hello, file readers!
Deep Dive
Java has evolved. Back in the day, you’d have to manage streams and readers yourself – plenty of boilerplate. The java.io
package was all the rage, with FileReader
and BufferedReader
often seen in the wild. Then came java.nio
, offering channels and buffers for more control.
Now, java.nio.file
is even higher level. Files
and Paths
simplify the job. The example above uses Files.lines
, which streams lines lazily, great for big files. You also get try-with-resources, automatically closing streams to avoid leaks.
Alternatives? Scanner
is handy for parsing. Apache Commons IO and Google’s Guava have utilities for more complex tasks, if you need them. Still, vanilla Java usually gets you pretty far.
Implementation-wise, file encoding matters. Files.lines
assumes UTF-8 by default but you can specify another. On the other hand, BufferedReader
needs you to set the Charset
upfront if it’s not the default.
See Also
For more zest, peek at these:
- The
Files
class in Java’s official documentation. - Reading, Writing, and Creating Files for a thorough walk-through.
- Apache Commons IO for a robust library of file IO utilities.