In this tutorial, you will learn how to read a text file in Java. First, you will learn how to achieve this using the readLine()
method of BufferedReader
. Second, you will use the readAllLines()
method of the Files
class. The last step will show how to use the readLines()
method of FileUtils
.
Create a new project
Follow through the article linked below to create a new Maven project using IntelliJ.
Add project dependencies
To use the FileUtils class, you have to add the Commons IO dependency. Copy and paste the following XML into the file named pom.xml.
<!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.13.0</version> </dependency>
Next, press CTRL+SHIFT+O to load the Maven changes. This will download the Commons IO dependency and add it to your project’s classpath.
Create a text file
Create a file named data.txt under the root package of your project. Copy and paste the following line of text into the file.
Hello JavaWhizz!
This text gets logged to the console for each of the methods that read the file without any errors. If you create the file in a different directory, make sure you provide the correct path to the file. Otherwise, you will get a FileNotFoundException
. For this tutorial, ensure your project is as shown below.

Using BufferedReader
In the src/main/java/com/javawhizz package, create a file called UsingBufferedReader.java. The code below should be copied and pasted into the file.
package com.javawhizz; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class UsingBufferedReader { public static void main(String[] args) { File fileLocation = new File("data.txt"); try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileLocation));){ String fileContents; while ((fileContents = bufferedReader.readLine()) != null){ System.out.println(fileContents); } }catch (IOException e){ e.printStackTrace(); } } }
An effective method of reading from files is offered by the BufferedReader
class. As a result, it employs a default buffer size rather than a direct file read to buffer characters from the file.
Create a new instance of the class and supply a Reader
as the parameter if you want to read from a file. Pass an instance of FileReader
as the argument since you are reading from a file. The FileReader
instance’s parameter should be the file path.
The software will then be able to locate the file. After that, read the contents of the file using BufferedReader’s readLine()
method. In this instance, the software creates a temporary String object and puts the file’s contents in it.
Once null is returned, the while loop comes to an end. This demonstrates that the software has read every line in the file. The results listed below should appear if your application works smoothly.
Output
Hello JavaWhizz!
Using the readAllLines() method
Make a UsingAllLinesMethod.java file in the src/main/java/com/javawhizz package. Copy and paste the following code into the file.
package com.javawhizz; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; public class UsingReadAllLinesMethod { public static void main(String[] args) { Path filePath = Path.of("data.txt"); try { List<String> strings = Files.readAllLines(filePath); strings.forEach(System.out::println); }catch (IOException e){ e.printStackTrace(); } } }
The static methods offered by the Files
class can be used when working with files. In this illustration, the software reads every line from the data.txt file using the readAllLines()
method.
Since a Path
object is what the method requires for the file. To obtain the file path, use the Path.of()
method. The Path object should then be passed to the readAllLines()
method as an argument. Thus, for each line read from the file, the procedure should return a List
of strings.
To log the contents of the file to the console, use the forEach()
method on the List
that was returned.
Output
Hello JavaWhizz!
Using the readLines() method
Make a UsingReadLinesMethod.java file in the src/main/java/com/javawhizz package. Copy and paste the following code into the file.
package com.javawhizz; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.util.List; public class UsingReadLinesMethod { public static void main(String[] args) { File fileLocation = new File("data.txt"); try { List<String> strings = FileUtils.readLines(fileLocation); strings.forEach(System.out::println); }catch (IOException e){ e.printStackTrace(); } } }
The FileUtils
class from the Commons IO package is quite identical to the Files
class. Both of these classes provide static methods for working with files.
In this example, the program uses the readLines()
method to read the lines in the file. You should pass an instance of a File
as the argument to specify the file location.
The method returns a List
of strings after reading from the file. Call the forEach()
method of the List
to log the contents of the file to the console.
Output
Hello JavaWhizz!
Conclusion
You have now mastered the Java technique for reading from a file. First, you become familiar with BufferedReader’s readLine()
method. Second, you now know how to use the Files
class’ readAllLines()
method. The readLines()
method of the FileUtils
class was covered in the previous manner.
Happy Hacking!
0 Comments