All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Java Read From File Line By Line

Last Updated : Mar 11, 2024

Java Read From File Line By Line

In this article we will show you the solution of java read from file line by line, a readLine() method from java.io.BufferedReader is available for Line-by-line file reading to a String.

When the file is finished, this function returns null. Comparing the Java Scanner class to the BufferedReader class, more utility methods are offered.

The nextLine() function of the Java Scanner class makes it simple to read a file's contents line by line.

The same String is returned by the nextLine() method as by readLine().

The Scanner class additionally has an InputStream file reading capability. Now move to the concept of java read from file line by line.

Step By Step Guide On Java Read From File Line By Line :-

The most popular and straightforward method for reading the file by line in Java is by using the BufferedRedaer class.

The java.io package contains it. ReadLine() is a method provided by the Java BufferedReader class that allows for line-by-line file reading.

The line of text is read using the procedure. It gives back a string that contains the line's content.

import java.io.*;
public class TkReadLineByLineEx1
{
public static void main(String args[])
{
try
{
File file=new File("Talkerscode.txt");
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
StringBuffer sb=new StringBuffer();
String line;
while((line=br.readLine())!=null)
{
sb.append(line);
sb.append("\n");
}
fr.close();
System.out.println("Data of File: ");
System.out.println(sb.toString());
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
  1. The package java.io is imported on the initial line of code. It offers a number of input streams as well as a number of output streams for reading and writing data to and from files and other input as well as output sources.
  2. Finally, using the public static void main function, we build the public class known as ReadLineByLineEx1.
  3. The try statement is then used, allowing you to specify a piece of code that will be checked for problems as it is being performed.
  4. After that, a fresh file instance is created, and the file name is supplied when the File object is initialised.
  5. Next, we utilise the filereader object to read the file. This object enables web applications to asynchronously read files on the user's machine.
  6. In order to read text from the a character-based input stream, we next employed the Java BufferedReader class. By using the readLine() method, data can be read line by line.
  7. The Java String lines() method is then used to return a stream of lines from string.
  8. By using the BufferedReader, we then apply the while statement. The readLine() method returns null when it has finished reading the file. Before it reaches the file's end, your software seems to be reading lines from the file.
  9. In order to add lines to a string buffer & insert line feeds, we then use the append method.
  10. The stream is then shut off, and the resources are released.
  11. The statement corresponding to the file's contents is then printed.
  12. After that, a string textually representing the item is returned.
  13. Next we use a catch block to prevent an IOException, which serves as the base class to errors that can occur while accessing data through streams, files, or directories.
  14. That throwable and also its backtrace are then printed to a standard error stream using the printStackTrace() method.

Conclusion :-

Hence, we were able to understand how Java reads a file line by line. Also, we discovered that java.io has a readLine() method.

For reading a file line by line and writing the results to a String, use BufferedReader. That function returns null when the file is complete.

There are more utility methods provided in the Java Scanner class in comparison to the BufferedReader class.

I hope this article on java read from file line by line helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪