When the files are created and the content is written to the files, there arises the need to read the files in Java. There is a certain application that requires the reading of a file. The files in Java are created by declaring the object and specifying the file name with its location. There are several approaches available to read and write to the files in Java.
In this article, we will elaborate on different methods to read the contents of a file in Java.
How to Read a File in Java?
After the content is written to the files using various methods. At times there arises a need to read the content or the information present in the files. Java is a programming language that implements different methods to read a file.
There are certain methods in Java that help users effectively read a file are listed below:
- Using Scanner class
- Using BufferedReader class
- Reading the whole file in a List
- Using File Reader class
- Read a text file as a String
Before implementing the above methods, we will declare a sample file as below:
Declaration of the File as “code1.txt”
The screenshot below shows that a sample file is created as “code1.txt” in D drive.

Now we will discuss in detail the implementation of these methods to read a file.
Method 1: Using Scanner Class
Scanner is a built-in Java class that offers numerous functions to take input from different sources like files and keyboards. The code below uses the scanner class of Java to read the file.
//Import the required package
import java.io.File;
import java.util.Scanner;
//Declare a class
class Method1 {
// main() method of Java
public static void main(String[] args) throws Exception {
// The path where the file is located is declared.
File filepath = new File("D:/code1.txt");
Scanner scan = new Scanner(filepath);
while (scan.hasNextLine())
System.out.println(scan.nextLine());
// Close the Scanner object
scan.close();
}
}
In the code above:
- The package is imported according to the requirement.
- A class is declared as “code1.txt” to read the file in Java.
- The object for the file is created and the path of the file is specified.
- The file is read by the Scanner object “scan” using the hasNext() method that reads the line in the file.
- scan.close() method closes the scanner.
Output
The output below shows that the entered input in the file is printed using the Scanner class.

Method 2: Using BufferedReader Class
It is a built-in class in Java that efficiently reads the character from the input stream. It is frequently utilized with the input sources like files or streams to efficiently process the text. The below code depicts the implementation of the BufferReader class to read the files in Java.
// Import input-output package
import java.io.*;
//Class to read a file in Java
class Method2 {
// main() method of Java
public static void main(String[] args) throws Exception {
// The location of the file is entered
File fileread = new File("D:/code1.txt");
// Create an object of the BufferedReader class
BufferedReader readfile = new BufferedReader(new FileReader(fileread));
// Declaration of string
String str1;
// Condition is true when there is a string otherwise false
while ((str1 = readfile.readLine()) != null)
// Print the string in the file
System.out.println(str1);
readfile.close();
}
}
In the above code block:
- The input-output package is imported.
- The class is declared as “BufferdReaderclass” with an object created as “fileread” which contains the file location.
- In the next step, a readfile object for BufferedReader is created to read the selected file.
- The while loop is executed along with the readfile.readLine() to read all the content of the selected file and store it in the “str1”.
- The strings in the file are printed using the println() method.
Output
The output below describes that the strings in the file code1.txt are read successfully.

Method 3: Reading a Whole File From the List
This method reads all the lines present in the file. The below code depicts the implementation of the readAllLines() method to read the file in Java.
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;
//Declare a class for the method
class Method3 {
public static List<String> readFileInList(String file) {
// The object created for the List
List<String> readlines = Collections.emptyList();
try {
// Read the lines in the file
readlines = Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8);
}
// The Input Output exception
catch (IOException e) {
e.printStackTrace();
}
return readlines;
}
// The main() method of Java
public static void main(String[] args) {
// The location of the file is in the D drive
List li = readFileInList("D:/code1.txt");
// Each character is iterated over
Iterator<String> i = li.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}
In the above Java code:
- The packages are imported.
- Initially, an empty list is created with no elements declared as “empty.List()” in Java.
- The try block reads all the lines present in the declared file in Java.
- The catch block is used for any input-output exception.
- The next step involves the main method of Java that contains the file with its location in the list.
- The iterator() method iterates over the strings in the file and returns the output accordingly.
Output
The output below shows that the strings in the file are printed using the readAllLines () method of Java.

Method 4: Reading a File using the FileReader
The File Reader constructor belongs to the Convenience Class of Java. This constructor is used to read the contents of the file and the code below depicts the implementation of the FileReader in Java.
//Import the required package to read the class
import java.io.*;
//Declare the class for reading the file.
class Method4 {
// main() method of Java
public static void main(String[] args) throws Exception {
// Declaring the path of the file
FileReader meth1 = new FileReader("D:/code1.txt");
// The while loop in Java
int x;
// Condition remains true until all the information is read from the file
while ((x = meth1.read()) != -1)
// print the information in the file
System.out.print((char) x);
meth1.close();
}
}
In the above code block:
- The required package is declared along with the class as “Method4”.
- In the next step, the object “meth1” is created for the FileReader which consists of the file directory.
- The while loop is executed to loop through till the whole content is read from the file.
- The output is printed using the println() method.
- The meth1 is closed using the close() method in Java.
Output
The below outputs indicate that the contents in the file are printed using the FileReader constructor.

Method 5: Read a File as a String
The code below implements the readFileAsString() method and readAllBytes() method that reads the bytes in the file and returns the data in the form of the byte array.
//Import the package required
import java.nio.file.*;;
class Method5 {
//the method is used to read the file
public static String readFileAsString(String file)throws Exception {
String input = "";
//readAllBytes reads the data from the file
input = new String(Files.readAllBytes(Paths.get(file)));
return input;
}
//The main() method of Java
public static void main(String[] args) throws Exception {
//The path specified where the file is present
String input = readFileAsString("D:/code1.txt");
//print the output as the contents of the file.
System.out.println(input);
}
}
In the above code block:
- The required package is imported.
- The class declared as “Method5” contains the method “readFileAsString()” to read the contents of the file.
- The next step involves the method readAllBytes(), which basically reads the file and returns the data in a byte array.
- The path of the file is specified and declared in the String “input”.
- The output is printed using the println() method of Java.
Output
The below output in Java elaborates that the contents in the specified file “code1.txt” are printed.

Conclusion
To read a file in Java there are certain methods like the Scanner class, BufferedReader class, and FileReader constructor. We can also read the files in Java using List and as a string. In this article, we have abstracted the content of the file using different methods in Java.