Program to Read text from file using BufferedReader's read() and readLine() methods in java file IO

BufferedReader  >

  • BufferedReader is buffered.


  • when BufferedReader.read() is called mostly data is read from the buffer. When data is not available available in buffer a call is made to read system file and lot of characters are kept in buffer.
  • A BufferedReader enables another  Reader to buffer the characters and supports the mark and reset methods.
  • BufferedReader is much faster as compared to FileReader.
  • Example -
    • As we discussed above that when BufferedReader.read() is called mostly data is read from the buffer.
    • A BufferedReader reads from FileReader, will request lot of data from the FileReader (128 characters or so… not exact figure). Thus only 2 calls will be made for reading 256 characters from file.
  • Another Example - Real world Example - You must have seen youtube videos where video is buffered before you actually start watching it, buffering overall improves your video watching experience.



We will read c:/myFile.txt


c:/myFile.txt looks like this >
You are learning File IO from javaMadeSoEasy.com


In this post we’ll write >
  • Program 1 to Read text from file using BufferedReader's read() method in java file IO
  • Program 2 to Read text from file using BufferedReader's readLine() method in java file IO


Program 1 to Read text from file using BufferedReader's read() method in java file IO

BufferedReader's read() method returns int which can be casted to char.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class ReadFileUsingFileReader {
   public static void main(String... args) {
          FileReader fr = null;
          BufferedReader br = null;
          try {
                 fr = new FileReader("c:/myFile.txt");
                 br = new BufferedReader(fr);
                 System.out.println("Reading from text file using "
                              + "BufferedReader's read() method > ");
                 int ch;
                 while ((ch = br.read()) != -1) { //read till end of file
                       System.out.print((char) ch);
                 }
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (br != null)
                              br.close(); //close BufferedReader
                                            // will close FileReader
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/* OUTPUT
Reading from text file using BufferedReader's read() method >
You are learning File IO from javaMadeSoEasy.com   
*/




Program 2 to Read text from file using BufferedReader's readLine() method in java file IO

BufferedReader's readLine() method reads whole line and return it in  String form.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class ReadFileUsingFileReader {
   public static void main(String... args) {
          FileReader fr = null;
          BufferedReader br = null;
          try {
                 fr = new FileReader("c:/myFile.txt");
                 br = new BufferedReader(fr);
                 System.out.println("Reading from text file using "
                              + "BufferedReader's readLine() method > ");
                 String str;
                 while ((str = br.readLine()) != null) { //read till end of file
                       System.out.print(str);
                 }
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (br != null)
                              br.close(); //close BufferedReader
                                            // will close FileReader
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/* OUTPUT
Reading from text file using BufferedReader's readLine() method >
You are learning File IO from javaMadeSoEasy.com   
*/





RELATED LINKS>

Program to How to use Mark and Reset methods with BufferedReader

Difference between FileInputStream and BufferedInputStream in java file IO


Copy a file in java 2 in ways - use org.apache.commons.io.FileUtils's copyDirectory(sourceDirectory, destDirectory)


Program to Move a file - in java file IO


No comments:

Post a Comment