Read a File Line by Line in Java

Files can exist huge in size containing hundreds and thousands of lines of information and so instead of reading the unabridged file which volition have organization resources, we tin impress text files line by line using BufferedReader class in Java. So in this commodity, we will provide you with a thorough explanation along with a code that can be used to read your files. So, allow's get started

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

To read any file y'all need to initialize a file handling process in Java which you can practise by typing:

Next, we take imported a class that is used to read data from the keyboard when pressed past the user every bit shown below:

import java.util.Scanner ;

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

public class FileReaderLineByLine {

……

}

This will exist your principal part within a class where nosotros have passed the string argument:

public static void main( String [ ] args)

Now we are initializing a file with the name of 'Employees.txt' along with the path where it is located:

At present nosotros have initialized a file reader variable 'fr' that volition exist used to fetch the content of the whole file and initially prepare the value to null:

After that, we take likewise initialized a buffered reader which will be used to read the make full line by line and set its value also to null:

It is a compulsory step to implement the exception handling process hither as sometimes it is possible that you are reading a file that is huge or in example of whatever error occurs then the plan tin can be crashed which makes a file corrupt that you are trying to read. Then to do that you need to implement the 'attempt' and 'grab' process. Within the try body, you should read the file for security purposes as shown beneath:

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

Information technology is really important to read files line by line because sometimes the file has a huge size and you can't read the whole file as this will have a cracking impact on the performance of your organisation. Now next is the catch part which will execute only if whatever fault 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 will bear witness you the whole code that we have explained in chunks in the previous part which is shown below:

import coffee.io.* ;
import coffee.util.Scanner ;
public class FileReaderLineByLine {
public static void main( Cord [ ] args) {
System.out.println ( "Reading file line-past-line using BufferedReader object\n" ) ;
System.out.println ( "" ) ;
File f= new File ( "Employees.txt" ) ;
FileReaderfr= nada ;
BufferedReaderbr = nothing ;
try
{
Cord line;
fr= new java.io.FileReader (f) ;
br= new BufferedReader (fr) ;
while ( (line=br.readLine ( ) ) != null )
{
System.out.println (line) ;
}
br.close ( ) ;
fr.shut ( ) ;
} //try
catch ( IOException ex) {
System.err.println ( "Mistake while reading file: " + ex.getMessage ( ) ) ;
} //take hold of
} //main()
} //class

If y'all desire to execute this java code in the Linux operating system then yous can exercise that by using whatsoever text editor of your choice. For case, nosotros are using a nano text editor and so nosotros will practise that by typing.

$ nano FileReaderLineByLine.java

The next step is to write the code and and then relieve information technology:

At present to execute this lawmaking y'all need to first make sure that you accept installed the coffee evolution kit (JDK) application in your Linux operating system by typing:

$ sudo apt install default-jdk

After that, yous need to compile the code starting time before trying to execute the program.

$ javac FileReaderLineByLine.java

$ coffee FileReaderLineByLine

Now yous can see that subsequently executing the file information technology is reading data that is bachelor in the text file of "Employees.txt" which is shown below:

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

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

There is one more than method to read files line by line is by using a Scanner form in java and for that, you need to blazon.

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

public static void principal( Cord [ ] args) {
try {
Scanner scanner = new Scanner( new File ( "Employees.txt" ) ) ;
while (scanner.hasNextLine ( ) ) {
Arrangement.out.println (scanner.nextLine ( ) ) ;
}
scanner.close ( ) ;
} catch ( FileNotFoundException e) {
due east.printStackTrace ( ) ;
}
}

}

Conclusion

Sometimes Files can be huge in size containing hundreds and thousands of lines of information. Instead of reading the unabridged file which will take system resources, we can read them line by line using the BufferedReader class in Java. In this article, we have taught you how you can read whatever file using a Coffee programming language and for that, y'all demand to import the file handling process. You lot too demand to implement the effort and catch process which is used to handle the file exceptions in case of any error while reading a file which has been discussed in this article.

andersonaggland.blogspot.com

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

0 Response to "Read a File Line by Line in Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel