How to read file using BufferReader.




This  program  simply  explains  how to  read file form given  file path . Using  bufferReader
 this  we can  read  file .   Vary  simple  use of  io and util  package of java 7. You compile
 this code in your machine to get output.





import java.io.BufferedReader; 
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
**
 *   @author  Mukesh Jha
 *
 * 

 */


public class Bufferreader {

  public static List<String> readLines(File filethrows Exception {
      if (!file.exists()) {
          return new ArrayList<String>();
      }
      BufferedReader reader = new BufferedReader(new FileReader(file));
      List<Stringresults = new ArrayList<String>();
      String line = reader.readLine();
      while (line != null) {
          results.add(line);
          line = reader.readLine();
      }
      return results;
     }
  
  
      public static void  main (String str[]) {
  
          System.out.println("Input your File Path");
         
            Scanner  scn=new Scanner(System.in);
  
            File  fileInput =new  File (scn.nextLine());
  
            
            try {
List <StringfileRead=readLines(fileInput);
  forString strm:fileRead) {
  
  
   System.out.printlnstrm);
   
  }
    catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  
        }
  
  
}

Programm  output :


   Input your File Path

/Users/yourpath/Resume9EXPJava.docx
How to read file using BufferReader. How to read file using BufferReader. Reviewed by Mukesh Jha on 12:08 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.