Java Program to Read the Content of a File Line by Line
July 28, 2023
What is Java file and File Reader class.
The Java File class abstractly describes the pathnames to files and directories. The creation of files and directories, file searching, file deletion, etc. are all performed using this class. The real file or directory on the disc is represented by the File object.
To read character data from a file, Java’s FileReader class, a subclass of InputStreamReader, is utilised. It is one of the various methods available in Java for reading files, and it is frequently used to read text data from files.
To know more about java Program to Read the Content of a File Line by Line read the complete article.
How to read the contents of a file in Java?
There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader, or Scanner to read a text file.
Methods to read the content of a file in java:
Using BufferedReader class. – Java has a class called BufferedReader that is used to read text from a character input stream. To read data from a file or network stream, the BufferedReader class from the java.io package is frequently used in conjunction with an underlying Reader implementation, such as FileReader or InputStreamReader.
Using Scanner class – Java’s Scanner class has methods for reading input of different sorts from sources such input streams, files, and strings. It is frequently used to parse data from strings, read data from files, and read user input from the command line. Additionally, Scanner offers approaches for dealing with other forms of input, such as regular expressions.
Using File Reader class – Java’s FileReader class offers a simple way to read characters from a file. Although the FileReader class is helpful for reading simple text files, it has numerous drawbacks, including the inability to handle big files effectively and the absence of capability for reading data in binary format.
To retrieve file content line by line, use the readLine() function. Each time readLine() is used, a line is returned.
.java file: A . java file contains your Java source code
.class in Java: A . class file contains the Java bytecode produced by the Java compiler.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
File file = new File("example.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("PrepInsta Prime.");
}
}
}
Output
PrepInsta Prime
Explanation:It should be noted that this example assumes you have a file named example.txt in the same directory as the application, and that the file contains text with line breaks between each paragraph. A FileNotFoundException will be raised if the file is missing.
Example 2 : Reading the Content of a File line by line using BufferedInputStream
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
try {
InputStream input = new BufferedInputStream(new FileInputStream("example.txt"));
byte[] buffer = new byte[1024];
int bytesRead;
StringBuilder sb = new StringBuilder();
while ((bytesRead = input.read(buffer)) != -1) {
String line = new String(buffer, 0, bytesRead);
sb.append(line);
}
String[] lines = sb.toString().split("\n");
for (String line : lines) {
System.out.println(line);
}
input.close();
} catch (IOException e) {
System.out.println("Use PrepInsta Prime for learning Java.");
}
}
}
Output
Use PrepInsta Prime for learning Java.
Explanation:The bytesRead variable keeps track of how many bytes were really read as the data is read into a buffer using the read() method. The data is first combined into a single string using the StringBuilder class, which is then divided into several lines using the split() function and the n line break character. The console is then printed with each line.
Example 3: Reading the Content of a File line by line Using Scanner
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(new File("example.txt"));
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("Content of a File line by line Using Scanner.");
}
}
}
Output
Content of a File line by line Using Scanner.
Explanation:Data from the file is read using the Scanner. The nextLine() method is used to read each line of text, and the hasNextLine() method is used to check whether there is another line of text to be read. When all lines have been read, the while loop ends and hasNextLine() returns false. The console is then printed with each line. Finally, the Scanner is closed and whatever resources it used are released by calling the close() method.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment