Skip to content Skip to sidebar Skip to footer

How to Read a File Line by Line Java

Files can exist huge in size containing hundreds and thousands of lines of information so instead of reading the entire file which will accept organisation resources, we can impress text files line by line using BufferedReader class in Java. So in this article, we will provide yous with a thorough explanation along with a lawmaking that tin can exist used to read your files. So, let's get started

Reading files line-by-line using BufferedReader form in Java

To read whatsoever file you lot need to initialize a file treatment process in Java which you can do by typing:

Next, we have imported a grade that is used to read data from the keyboard when pressed by the user as shown below:

import java.util.Scanner ;

After that, nosotros have created a public class where all our coding will reside by typing:

public class FileReaderLineByLine {

……

}

This will be your main function inside a class where nosotros have passed the string argument:

public static void master( String [ ] args)

Now nosotros are initializing a file with the proper name of 'Employees.txt' forth with the path where it is located:

At present we have initialized a file reader variable 'fr' that will exist used to fetch the content of the whole file and initially set the value to goose egg:

Subsequently that, we accept as well initialized a buffered reader which will be used to read the fill line by line and prepare its value also to zippo:

It is a compulsory stride to implement the exception treatment process here every bit sometimes it is possible that you are reading a file that is huge or in case of any mistake occurs then the program can be crashed which makes a file decadent that you are trying to read. And so to exercise that you need to implement the 'endeavour' and 'catch' procedure. Inside the attempt body, you should read the file for security purposes as shown beneath:

try
{
fr= new java.io.FileReader (f) ;
br= new BufferedReader (fr) ;
while ( (line=br.readLine ( ) ) != null )
{
System.out.println (line) ;
}
br.shut ( ) ;
fr.close ( ) ;
}

It is really important to read files line past line because sometimes the file has a huge size and you tin't read the whole file as this will have a corking touch on the performance of your system. At present next is the take hold of function which will execute merely if any error occurs during the file reading process and its syntax is as follows.

catch ( IOException ex) {
System.err.println ( "Error while reading file: " + ex.getMessage ( ) ) ;
}

Now, this is where we volition show yous the whole code that we accept explained in chunks in the previous role which is shown below:

import java.io.* ;
import java.util.Scanner ;
public class FileReaderLineByLine {
public static void main( String [ ] args) {
System.out.println ( "Reading file line-by-line using BufferedReader object\n" ) ;
System.out.println ( "" ) ;
File f= new File ( "Employees.txt" ) ;
FileReaderfr= zilch ;
BufferedReaderbr = zero ;
endeavour
{
String line;
fr= new java.io.FileReader (f) ;
br= new BufferedReader (fr) ;
while ( (line=br.readLine ( ) ) != cipher )
{
System.out.println (line) ;
}
br.shut ( ) ;
fr.close ( ) ;
} //try
catch ( IOException ex) {
Organization.err.println ( "Mistake while reading file: " + ex.getMessage ( ) ) ;
} //catch
} //primary()
} //class

If y'all want to execute this java code in the Linux operating arrangement then y'all can do that by using whatsoever text editor of your choice. For instance, nosotros are using a nano text editor then we will do that by typing.

$ nano FileReaderLineByLine.java

The next step is to write the lawmaking and then save it:

Now to execute this code you need to first make certain that you accept installed the coffee evolution kit (JDK) application in your Linux operating system by typing:

$ sudo apt install default-jdk

Afterward that, you lot need to compile the code start before trying to execute the programme.

$ javac FileReaderLineByLine.java

$ coffee FileReaderLineByLine

Now yous tin can run across that after executing the file it is reading data that is available in the text file of "Employees.txt" which is shown beneath:

Note: You demand to open a terminal and so access the same directory where this text file is present otherwise you won't exist able to read this file.

Reading files line-by-line using Scanner class in Java

There is 1 more than method to read files line by line is by using a Scanner grade in java and for that, you demand to type.

import java.io.File ;
import java.io.FileNotFoundException ;
import coffee.util.Scanner ;
public grade ReadFileLineByLine {

public static void main( String [ ] args) {
attempt {
Scanner scanner = new Scanner( new File ( "Employees.txt" ) ) ;
while (scanner.hasNextLine ( ) ) {
System.out.println (scanner.nextLine ( ) ) ;
}
scanner.close ( ) ;
} catch ( FileNotFoundException e) {
e.printStackTrace ( ) ;
}
}

}

Decision

Sometimes Files tin can exist huge in size containing hundreds and thousands of lines of data. Instead of reading the entire file which will take organization resources, we can read them line past line using the BufferedReader class in Java. In this article, we have taught you how you can read whatever file using a Java programming language and for that, yous need to import the file handling process. You as well need to implement the endeavour and catch process which is used to handle the file exceptions in instance of any mistake while reading a file which has been discussed in this article.

nelsonfingir.blogspot.com

Source: https://linuxhint.com/read-file-line-by-line-java/

Post a Comment for "How to Read a File Line by Line Java"