There are several ways present in java to read the text file like BufferReader, FileReader, and Scanner. Each and every method provides a unique way of reading the text file.
Methods:
- Using Files class
- Using FileReader class
- Using BufferReader class
- Using Scanner class
Let’s see each and every method in detail with an example to get a better understanding of the methods to, later on, implement the same to extract the content from a text document.
Method 1: Using Files class
As Java provides java.nio.file. API we can use java.nio.file.Files class to read all the contents of a file into an array. To read a text file we can use the readAllBytes() method of Files class with which using this method, when you need all the file contents in memory as well as when you are working on small files.
Example:
Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class GFG {
private static void readUsingClass(String fileName)
throws IOException
{
Path path = Paths.get(fileName);
byte [] bytes = Files.readAllBytes(path);
System.out.println(
"Read text file using Files class" );
@SuppressWarnings ( "unused" )
List<String> allLines = Files.readAllLines(
path, StandardCharsets.UTF_8);
System.out.println( new String(bytes));
}
public static void main(String[] args)
throws IOException
{
String fileName
= "/Users/mayanksolanki/Desktop/file.txt" ;
readUsingClass(fileName);
}
}
|
Output:

Method 2: Using FileReader class
We can use java.io.FileReader can be used to read data (in characters) from files. This is a very efficient method to read the file line by line.
Syntax:
FileReader input = new FileReader(String name);
Example:
Java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class GFG {
private static void readUsingFileReader(String fileName)
throws IOException
{
File file = new File(fileName);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
System.out.println(
"Reading text file using FileReader" );
while ((line = br.readLine()) != null ) {
System.out.println(line);
}
br.close();
fr.close();
}
public static void main(String[] args)
throws IOException
{
String fileName
= "/Users/mayanksolanki/Desktop/file.txt" ;
readUsingFileReader(fileName);
}
}
|
Output:

Method 3: Using BufferedReader class
If you want to read the file line by line and If you want to process on that file then you have to use BufferedReader. It is also used for processing the large file, and it supports encoding also. Reading operation is very efficient on BufferReader.
Note: Either specify the size of the BufferReader or keep the size as a Default size of BufferReader which is 8KB.
Syntax:
BufferedReader in = new BufferedReader(Reader in, int size);
Implementation:
Hello I am learning web- development.
I am writing article for GFG.
I am cloud enthusiast.
I am an open-source contributor.
Note: Before starting create a text file by using .txt extension on your local machine and use the path of that file whenever you necessary while practicing.
Example:
Java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class GFG {
private static void
readUsingBufferedReader(String fileName)
throws IOException
{
File file = new File(fileName);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
System.out.println(
"Read text file using BufferedReader" );
while ((line = br.readLine()) != null ) {
System.out.println(line);
}
br.close();
fr.close();
}
public static void main(String[] args)
throws IOException
{
String fileName
= "/Users/mayanksolanki/Desktop/file.txt" ;
readUsingBufferedReader(fileName);
}
}
|
Output:

Method 4: Using Scanner class
If we want to read the document based on some expression & If you want to read the document line by line then we use Scanner class. A Scanner breaks the input into tokens, which by default matches the white space.
Example :
Java
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
public class GFG {
private static void readUsingScanner(String fileName)
throws IOException
{
Path path = Paths.get(fileName);
Scanner scanner = new Scanner(path);
System.out.println( "Read text file using Scanner" );
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
}
public static void main(String[] args)
throws IOException
{
String fileName
= "/Users/mayanksolanki/Desktop/file.txt" ;
readUsingScanner(fileName);
}
}
|
Output:

Implementation:
Here “my_file.txt” is a demo file used for the program demonstrated below where sample lines are as follows:
Hello I am learning web- development.
I am writing article of GFG.
I am cloud enthusiast.
I am an open-source contributor.
Example 1:
Java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
public class read_file {
private static void readUsingFileReader(String fileName)
throws IOException
{
File file = new File(fileName);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
System.out.println(
"Reading text file using FileReader" );
while ((line = br.readLine()) != null ) {
System.out.println(line);
}
br.close();
fr.close();
}
private static void
readUsingBufferedReader(String fileName)
throws IOException
{
File file = new File(fileName);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
System.out.println(
"Read text file using BufferedReader" );
while ((line = br.readLine()) != null ) {
System.out.println(line);
}
br.close();
fr.close();
}
private static void readUsingScanner(String fileName)
throws IOException
{
Path path = Paths.get(fileName);
Scanner scanner = new Scanner(path);
System.out.println( "Read text file using Scanner" );
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
}
private static void readUsingClass(String fileName)
throws IOException
{
Path path = Paths.get(fileName);
byte [] bytes = Files.readAllBytes(path);
System.out.println(
"Read text file using Files class" );
@SuppressWarnings ( "unused" )
List<String> allLines = Files.readAllLines(
path, StandardCharsets.UTF_8);
System.out.println( new String(bytes));
}
public static void main(String[] args)
throws IOException
{
String fileName
= "C:\\Users\\HP\\Desktop\\my_file.txt" ;
readUsingClass(fileName);
readUsingScanner(fileName);
readUsingBufferedReader(fileName);
readUsingFileReader(fileName);
}
}
|
Example 2: Reading specific lines from a text file
Now If you want to read the specific lines from the given document then we use the BufferReader method. Depending on the file BufferReader() method is used so that our code works faster and efficiently. In this program, the Text file stored in BufferReader is a traverse through all the lines by using for loop, and when if the condition becomes true we will print that lines
Implementation:
“myfile.txt” is the demo file to be used.
This is the sample lines been contained in this file which is consisting of random arbitral words composed in lines
3D printing or additive manufacturing is a process of making three dimensional solid objects from a digital file.
The creation of a 3D printed object is achieved using additive processes. In an additive process an object is created by laying down successive layers of material until the object is created. Each of these layers can be seen as a thinly sliced cross-section of the object.
3D printing is the opposite of subtractive manufacturing which is cutting out / hollowing out a piece of metal or plastic with for instance a milling machine.
3D printing enables you to produce complex shapes.
It uses less material than traditional manufacturing methods.
Java
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class GFG {
public static void main(String[] args)
{
String text = "" ;
int lineNumber;
try {
FileReader readfile = new FileReader(
"C:\\Users\\HP\\Desktop\\Exam.txt" );
BufferedReader readbuffer
= new BufferedReader(readfile);
for (lineNumber = 1 ; lineNumber < 10 ;
lineNumber++) {
if (lineNumber == 7 ) {
text = readbuffer.readLine();
}
else {
readbuffer.readLine();
}
}
}
catch (IOException e) {
e.printStackTrace();
}
System.out.println( " The specific Line is: "
+ text);
}
}
|
Output: The specific Line is:
3D printing enables you to produce complex shapes.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!