When working with files in Java, reading the content is a fundamental operation. Java provides multiple methods to read both text and binary files.
- Using
FileReaderandBufferedReaderto Read Text Files - Using
FileInputStreamto Read Binary Files - Using
Files.readAllLines()(NIO) - Using the
ScannerClass to Read Files
Reading Text Files with FileReader and BufferedReader
For reading text files, the combination of FileReader and BufferedReader is frequently used. FileReader is designed to read files character by character, while BufferedReader improves performance by buffering the input, allowing us to read the file more efficiently.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.io.*; public class FileReaderExample { public static void main(String[] args) { try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } |
FileReaderis used to open and read theexample.txtfile.BufferedReaderis wrapped aroundFileReaderto read the file line by line.- The
readLine()method reads each line of the file until the end is reached.
Reading Binary Files with FileInputStream
If we need to read binary data, such as images or multimedia files, FileInputStream is the go-to option. This class reads raw byte data, making it suitable for non-text files.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.io.*; public class FileInputStreamExample { public static void main(String[] args) { try (FileInputStream inputStream = new FileInputStream("example.bin")) { int byteData; while ((byteData = inputStream.read()) != -1) { System.out.print((char) byteData); // Cast to char for readable output } } catch (IOException e) { e.printStackTrace(); } } } |
FileInputStreamopens and reads the fileexample.binbyte by byte.- The
read()method returns a byte of data or-1when the end of the file is reached.
Reading Files with Files.readAllLines()
Introduced in Java 7, NIO (New I/O) provides modern utilities for working with files. The Files.readAllLines() method allows us to read the entire content of a text file into a List<String>. This is particularly useful when we want to load all lines at once into memory.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.nio.file.*; import java.io.IOException; import java.util.List; public class FilesReadAllLinesExample { public static void main(String[] args) { try { List<String> lines = Files.readAllLines(Paths.get("example.txt")); lines.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
Files.readAllLines()reads all the lines ofexample.txtinto aList<String>.- This method is convenient for smaller files, but might not be efficient for very large files since it loads the entire file into memory.
Reading Files with the Scanner Class
The Scanner class is versatile and can be used to read data from a variety of sources, including files. It’s especially helpful when we need to parse data or break it down into tokens.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.io.*; import java.util.Scanner; public class ScannerExample { public static void main(String[] args) { try (Scanner scanner = new Scanner(new File("example.txt"))) { while (scanner.hasNextLine()) { System.out.println(scanner.nextLine()); } } catch (FileNotFoundException e) { e.printStackTrace(); } } } |
Scannerreads the fileexample.txtline by line.- It’s easy to use when you want to read data and process or tokenize it on the fly.