Reading a text file in Java with the Scanner

As a Java developer it is a very common task to read file contents to a String object. It is also very common in pre-interview written tests read the contents of a file and apply regex to split string, etc.

4 things to watch-out for in File processing

1) Files must be closed once read. “Try with resources” feauture in java 7 is used to auto close the file once read.
2) Favor reading from a classpath over loading from an absolute path.
3) Scanner is for ASCII files, and a line-oriented scanner cannot be used for binary files. You have no guarantee that the binary file even has “lines” delimited by newline characters.
4) Reading large files directly into memory can cause memory issues. Read Processing large files efficiently in Java

Java Scanner class in action

#1. Scanner class reading from an absolute file path

text file to read

readme.txt

JDK 7 or later must be used to take advantage of the try with resources that auto closes the file.

The “java.util.regex.Pattern class states:

“\Z” The end of the input but for the final terminator, if any.
“\z” The end of the input.

The extra “\” is added to escape, since backslash is a special character in Java String. For e.g. to print a \ or ” which are special in string literal you have to escape it with another \ which gives us \\ and \”. Similarly, you need to escape “\” in “\Z” with another “\” which becomes “\\Z“.

#2. Scanner class dynamically constructing an absolute path

#3. Scanner class reading from the classpath relatively

The approaches #1 and #2 of reading a file via absolute path is not recommended because if the you move the deployed files to some other location then you will get “FileNotFoundException”. A better approach is to read from your classpath. The method “getResourceAsStream” in the java.lang.Class API to the rescue.

Why is this a better approach?

If you build a jar or war packaging of the above “MyFileReader” and “readme.txt” it can be deployed anywhere. For example, let’s build a jar file with Maven.

Step 1: Create a Maven Jar project

Step 2: Create the files MyFileReader.java & readme.txt

src/main/java: com.read.file.MyFileReader.java
src/main/resources: com.read.file.readme.txt

The “src/main/resources” folder can be created with right mouse click on “readFileWithScanner” and then “new –> Source Folder” and then typing “src/main/resources” as the source folder.

After importing into eclipse looks like:

Java Scanner to read File

Java Scanner to read File

Step 3: Ensure that the pom.xml uses Java 7 or later

to take advantage of the “Try with resources” feature.

Step 4: Build the jar file

Step 5: The built jar file looks like

jar file with the readme.txt and reader class file

jar file with the readme.txt and reader class file

Step 6: Copy this built jar file to say c:\temp folder and run

So, you can run this jar file in any folder as the lookup of the file is relative to the classpath.

#4. 2 Scanners: one for reading the file line by line & the other for tokenizing the line on spaces

The output:

You may also like other File I/O posts:

1) Processing large files efficiently in Java

2) 15 Java I/O interview Q&A


800+ Java Interview Q&As Menu

Learn by categories on the go...
Learn by categories such as FAQs – Core Java, Key Area – Low Latency, Core Java – Java 8, JEE – Microservices, Big Data – NoSQL, Architecture – Distributed, Big Data – Spark, etc. Some posts belong to multiple categories.
Top