You need to read a file into a Java application. How can you do this?
If you read the Sentry answer to How to Write to a File in Java, you’ll know there are many solutions to writing to files in Java. Similarly, there are many ways you can read from a file into a Java application, and each method has unique properties that make it more or less useful for your file-reading circumstances. We’ll take a look at three ways you can read a file in Java.
Files
class.BufferedReader
.Scanner
.Files
ClassThe Files
utility class has a very useful readAllLines()
method that will read all the lines of a file. The method signature denotes that the lines will be read into a simple List
of String
elements. If decoding the contents of the file into the standard UTF-8 charset is unsuitable, an overloaded method allows you to provide your own Charset
. According to the Java documentation, readAllLines()
is meant for simple ad-hoc file reading, so avoid using it for reading in large files.
Here’s an example showing how to read in a file using this method:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class ReadFile {
public static void main(String[] args) {
Path path = Paths.get("example.txt");
try {
List<String> lines = Files.readAllLines(path);
} catch (IOException ex) {
// handle exception...
}
}
}
If you need to read bytes from a file (rather than lines), the readAllBytes()
method can be used to read the file into a byte array:
byte[] fileBytes = Files.readAllBytes(path);
BufferedReader
ClassThe BufferedReader
class is an intuitive and performant approach that you can use to read character-oriented files into your Java applications. Buffering is efficient, so BufferedReader
is a good choice for reading larger text files line by line.
Writing the code for a BufferedReader
is a little bit more lengthy than the Files
method. It is good practice to close resources once we have finished using them, or as in the example below, use the try-with-resources construct where the reader is closed for us:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = br.readLine()) != null) {
// process the line (e.g. add to a List)
}
} catch (IOException e) {
// handle exception...
}
}
}
Scanner
ClassThe methods we’ve looked at so far indiscriminately read line by line from a file. The Scanner
class provides us with a way to read from files piece by piece. The Scanner
works by separating the contents of a file into pieces using a delimiter, so it’s best for reading files with content that is separated by some constant value. This could be a common comma separated file, for example, but the Scanner
supports any value for the delimiter.
Let’s take a look at a more unusual delimited file to illustrate this point. You have a file with the following contents that you would like to read into a List
:
lion&& tiger&& leopard&& lynx
Using a Scanner
, we can read the contents of the file into a List
as follows:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) throws IOException {
List<String> animals = new ArrayList<>();
Scanner scanner = new Scanner(new File("example.txt"));
scanner.useDelimiter("&& ");
while (scanner.hasNext()) {
String next = scanner.next();
animals.add(next);
}
scanner.close();
}
}