Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Javafile Java Read File Line By Line, Video Tutorial of Java Read File Line By Line

Related Tutorials


 
 

Share on Google+Share on Google+

Java Read File Line By Line, Video Tutorial of Java Read File Line By Line

Advertisement
This Video Tutorial teaches you how to read file in Java. If you are searching for Java Read File Line By Line then this tutorial is for you.

Video Tutorial and example for the most searched topic "Java Read File Line By Line"

This video tutorial explains you the steps to create a program in Java programming language to read a text file line by line. The "Java Read File Line by line" is most searched topics by the Java developers. This means Java developers are using the Java for reading the file line by line. Reading a text file line by line is provided many benefits. Java provides the API for reading the text file line by line efficiently.

There are many situations where it is required to read the large text file ranging from 1 GB to 10 GB in size. In this situation you must read the file line by line and the process the one line at a  time as per your business needs.

Commonly developers are using the BufferedReader class to read the file line by line. The BufferedReader class reads the characters in a buffer thus increases the performance of the application. So, in your program it is advisable to use this feature of Java.

Following is the video tutorial that shows you how you can write code to read a text file in Java line by line.

Steps to create a test program for reading file line by line:

Step 1: Copy or create your text file and save in a directory

Step 2: Create a java file and save on your computer.

Step 3: Add the following code into your program:

import java.io.*;

public class ReadTextFileLineByLine{

	public static void main(String args[]){
		try{
		FileInputStream fstream = new FileInputStream("myfile.txt");
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader( new InputStreamReader(in));
		String line="";
		while((line = br.readLine()) != null){
		 System.out.println(line);
		}
		in.close();
		}catch(Exception e){
		System.out.println("Error while reading the file:" + e.getMessage());
		}
	}

}

Step 4: Save the above file and compile and run the code.

The important line of the program is line = br.readLine(), which reads the data from text file line by line.

This program is very useful as it can be used to read the data file of big size also. Developers are trying to find the example code for "Java Read file line by line". Code explained here is good enough to use in production environment with little modification to process large text file also.

Developers used the java.io.BufferedReader class to read files in console, JSP, Servlet, Struts, Spring etc..  based applications.

Most of the developers follows the following way to read the file line by line:

String line="";
while((line = br.readLine()) != null){
	System.out.println(line);
}

In the above code BufferedReader class is used, which has the readLine() method to read one line at a time from input stream.

The readLine() method of the BufferedReader class reads one line of text from the input stream and returns as a String. Here is the use of readLine() method:

String oneLine = br.readLine();

Import methods of the BufferedReader class:

close() - The close() method is used to close the input stream.

read() - The read() is used to read a single character from the stream.

readLine() - The readLine() method is very useful and it is used to read one line at a time.

Other method of reading file in Java line by line

The java.util.Scanner class can also be used to read a file in Java line by line.

Advertisement

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: February 10, 2013

Related Tutorials

Discuss: Video Tutorial - Java Read File Line By Line  

Post your Comment


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