Search Java Programs

Friday, February 5, 2010

Java read file line by line - Java Tutorial

Introduction
Lets understand some I/O streams that are used to perform reading and writing operation in a file. In the section of Java Tutorial you will learn how to write java program to read file line by line. 
Java supports the following I/O file streams.
  • FileInputstream
  • FileOutputStream 
FileInputstream:
This class is a subclass of Inputstream class that reads bytes from a specified file name . The read() method of this class reads a byte or array of bytes from the file. It returns -1 when the end-of-file has been reached. We typically use this class in conjunction with a BufferedInputStream and DataInputstream class to read binary data. To read text data, this class is used with an InputStreamReader and BufferedReader class. This class throws FileNotFoundException, if the specified file is not exist. You can use the constructor of this stream as:
FileInputstream(File filename);
FileOutputStream:
This class is a subclass of OutputStream that writes data to a specified file name. The write() method of this class writes a byte or array of bytes to the file. We typically use this class in conjunction with a BufferedOutputStream and a DataOutputStream class to write binary data. To write text, we typically use it with a PrintWriter, BufferedWriter and an OutputStreamWriter class. You can use the constructor of this stream as:
FileOutputstream(File filename);
 DataInputStream:
This class is a type of FilterInputStream that allows you to read binary data of Java primitive data types in a portable way. In other words, the DataInputStream class is used to read binary Java primitive data types in a machine-independent way. An application uses a DataOutputStream to write data that can later be read by a DataInputStream. You can use the constructor of this stream as:
DataInputStream(FileOutputstream finp);
The following program demonstrate, how the contains are read from a file.
import java.io.*;

public class ReadFile{
    public static void main(String[] args) throws IOException{
      File f;
    f=new File("myfile.txt");

      if(!f.exists()&& f.length()<0)
      System.out.println("The specified file is not exist");

      else{
         FileInputStream finp=new FileInputStream(f);
      byte b;
    do{
      b=(byte)finp.read();
      System.out.print((char)b);
    }
      while(b!=-1);
        finp.close();
        }
    }
  }

Output of the Program:
C:\nisha>javac ReadFile.java

C:\nisha>java ReadFile
This is a text file?
C:\nisha>
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[])
  {
      try{
    // 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
    in.close();
    }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

C:\nisha>
Download the code

No comments:

Post a Comment

Website Design by Mayuri Multimedia