Home Java Javafile How to Read file line by line in Java program
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

How to Read file line by line in Java program

Advertisement
Following program will help you to write a Java program using BufferReader and Scanner Class to read a file line by line.

Programmers have always found it difficult to read a big file (5-10 GB) line by line in Java. But there are various ways that can help read a larger file line by line without using much of the memory on the computer.

Using the Buffer Reader and readline() method one can read the data faster even on the computer having less amount of memory.

In the example of Java Read line by line below, a BufferedReader object is created that reads from the file line by line until the readLine() method returns null.

The returning of null by readLine() method of BufferedReader indicates the end of the file.

Let?s say you want to read a file with name ?Read file line by line in Java.txt? that includes a text. Now first thing a program has to do is to find the file. For this we use FileInputStream class.

FileInputStream class extends the java.io.InputStream. It reads a file in bytes.

BufferedReader class extends java.io.Reader. It reads characters, arrays, and lines from an input stream. Its constructor takes input stream as input. It is important to specify the size of the buffer or it will use the default size which is very large.

We have used the readLine() method of BufferedReader class that is used to read a complete line and return string line.

The other methods that can be used are:

read() method of BufferedReader class that reads a character from the input stream.

read(char[] cbuf, int off, int len) method of BufferedReader class reads characters and allots then in the part of an array.

DataInputStream Class extends java.io.FilterInputStream. It reads primitive data types from an underlying input stream.

Whenever a read request is made by Reader, a similar read request is made of the character or byte stream. Hence most of the times, a BufferedReader is wrapped around a Reader like FileReader or InputStreamReader.

The Reader buffers the input from a specific file. If they are not buffered every time a read() method or readline() method is invocated, they can convert the bytes read from the file into characters and then return it. This can be of ineffective.

To release the file descriptor at the end, close the stream like shown in example ?inputStream.close()?. The output of read file line by line in Java is printed on console.

Example of Read file line by line using BufferReader:

package ReadFile;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadFileLineByLine {
public static void main(String[] args) throws IOException {


FileInputStream stream = new FileInputStream("c://readFileLineByLineInJava.txt");

DataInputStream inputStream = new DataInputStream(stream);

BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

String string;

while ((string = br.readLine()) != null) {

System.out.println(string);
}

inputStream.close();


}

}

Read file line by line in Java using Scanner Class:

Besides the Buffer reader, using Scanner class of Java API is another way to read a file line by line in Java. It used hasNextLine() method and nextLine() method to read the contents of a file line by line.

The benefit of using Scanner class is that it has more utilities like nextInt(), nextLong(). Hence, the need to convert a string to integer is no longer required as it can directly reads the number from the file.

Example of Read file line by line using Scanner Class:

package ReadFile;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class ReadFileLineByLine {
public static void main(String[] args) throws IOException {

FileInputStream stream = new FileInputStream("c://ReadFileLineByLineInJava.txt");

DataInputStream inputStream = new DataInputStream(stream);

Scanner scanner = new Scanner(inputStream);

while (scanner.hasNextLine()) {

System.out.println(scanner.nextLine());

}

inputStream.close();

}
}
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 7, 2013

Related Tutorials

Ask Questions?    Discuss: How to Read file line by line in Java program  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com