Home Java Example Java Io Java read file line by line - Java Tutorial

Related Tutorials


Share on Google+Share on Google+

Java read file line by line - Java Tutorial

In the section of Java Tutorial you will learn how to write java program to read file line by line.
C:\nisha>javac ReadFile.java

C:\nisha>java ReadFile
This is a text file?

This program reads the bytes from file and display it to the user.

Download this Program

The another program  use DataInputStreams for reading textual input line by line with an appropriate BufferedReader.

import java.io.*;
class FileRead 
 public static void main(String args[])
  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
  while ((strLine = br.readLine()) != null) {
  // Print the content on the console
  System.out.println (strLine);
  //Close the input stream
  }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());

Output of the Program:

C:\nisha>javac FileRead.java 

C:\nisha>java FileRead
this is a file


Download the code


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: April 16, 2007

Related Tutorials

Discuss: Java read file line by line - Java Tutorial   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
December 6, 2011

i understant for this pgm very easy;
January 16, 2012
my idea

all you'r programs are perfect thank you very much for you'r help
November 16, 2012
best website

this is the best website