Tuesday, October 16, 2012

Reading a File Line by Line

import java.io.*;
public class FileRead {
        public static void main(String[] args) throws Exception {
                File file = new File("C:\\data\\LOG.txt");
                FileReader fileReader = null;
                BufferedReader  br =null;
                if (file.exists())
               {
                 try
                 {
                 fileReader = new FileReader(file);
                    br = new BufferedReader(fileReader);
                    String st = "";
                    while ((st = br.readLine()) != null) {
                                System.out.println(st);
                    }
                 }
                 catch (FileNotFoundException e)
                 {   
                  System.out.println("FileNotFoundException raised");
                 }
                 catch (EOFException e){   
                  System.out.println("EOFException raised");
                 }
                 catch (IOException e)
                 {
                  System.out.println("IOException raised");
                 }
                 finally
                 {
                  try
                  {
                   System.out.println("Closing Objects..!!");
                   fileReader.close();
                   br.close();
                  }
                  catch(IOException ioe)
                  {
                   System.out.println("IOException caught..!!");
                   ioe.printStackTrace();
                  }
                 }
               }
        }
}

No comments:

Post a Comment