Java Program to Read the Content of a File Line by Line
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.
Let’s look at a Java program to Reading the Content of a File line by line is used to perform an operation.
Example: Program to Read the Content of a File Line by Line
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
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.
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.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment