skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, September 15, 2012

Java Read File Line by Line

Posted by Admin at 5:40 AM – 0 comments
 


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());
  }
 }
}



Labels: Java File Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger