How to Read a Text File in Java
-
Use the
Scanner
Class to Read Files in Java -
Use the
BufferedReader
Class to Read Files in Java -
Use the
FileReader
Class to Read Files in Java
In almost every application, we deal with reading and writing text, so it is essential to real about to learn about it. There are several methods to read a plain text file in Java.
In the tutorial, we will learn how to read a text file in Java.
Use the Scanner
Class to Read Files in Java
The Scanner class can read files in Java. First, a File object is created that represents the path of your required file. The object of the Scanner class is created by passing the above File object.
The hasNext()
function checks if another line exists in a file and the nextLine()
function reads the given line.
See the code below.
import java.io.File;
import java.util.Scanner;
public class ScannerClassDemo {
public static void main(String[] args) throws Exception {
File doc = new File("C:\\Drive\\Learn.txt");
Scanner obj = new Scanner(doc);
while (obj.hasNextLine()) System.out.println(obj.nextLine());
}
}
Output:
File Reading Example
A FileNotFound
exception is thrown if the given file does not exist.
We can also do this by creating the object of InputStream
class and then passing it through Scanner
class.
For example,
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Scanner;
public class ScannerClassDemo {
public static void main(String args[]) throws Exception {
InputStream ins = new FileInputStream("C:\\Drive\\Learn.txt");
Scanner obj = new Scanner(ins);
while (obj.hasNextLine()) System.out.println(obj.nextLine());
}
}
Output:
File Reading Example
The advantage of using this method is that it offers parsing ability.
Use the BufferedReader
Class to Read Files in Java
BufferedReader
reads the text from a character-based input stream. Buffering is done for the efficient reading of arrays, lines & characters. If the buffer size is not specified, the default size is used, which is large enough for the use. We can use it to read files in Java.
BufferedReader
provides fast reading through buffering of data.
In the following example, we read the text from a file at some specified path.
import java.io.*;
public class ReadingTextusingBufferedReader {
public static void main(String[] args) throws Exception {
File doc = new File("C:\\Drive\\Learn.txt");
BufferedReader obj = new BufferedReader(new FileReader(doc));
String strng;
while ((strng = obj.readLine()) != null) System.out.println(strng);
}
}
Output:
File Reading Example
Use the FileReader
Class to Read Files in Java
The FileReader
class offers a convenient way to read plain text in java. In this class, the constructor method takes the default byte-buffer size and default character encoding as most appropriate.
We can use it to read files in Java, as shown below.
import java.io.*;
public class ReadingTextFile {
public static void main(String args[]) throws IOException {
File doc = new File("Demo.txt");
doc.createNewFile();
FileReader freader = new FileReader(doc);
char[] i = new char[100];
freader.read(i);
for (char j : i) System.out.print(j);
freader.close();
}
}
Output:
File Reading Example
The createNewFile()
function creates a new empty file. After the function call, if the file does not exist, it will return True and create an empty file with a specified name.