Sunday, 8 June 2014

Java program to read input from file

The following java program reads input from a given file line by line and prints the output on the console.

program code :

import java.io.*;
 public class ReadFile
 {
    public static void main(String[] args)
    {
    String file="inputfile.txt";
    try{
    FileReader f = new FileReader(file);
    BufferedReader bufferReader = new BufferedReader(f);
    String line;
// Read file line by line and print on the console
    while ((line = bufferReader.readLine()) != null)   {
            System.out.println(line);
    }
    bufferReader.close();
    }catch(Exception e){
            System.out.println("Error while reading file line by line");                     
    }

    }
  }

No comments:

Post a Comment