Read file in Java - FileInputStream and FileReader

In Java Read operation can be performed using FileIntPutStream and Filereder. For writing streams of character, Filereader is preferred and FileIntputStream is used for binary data like images.
FileReader -  Reading streams of characters 
FileInputStream -  Reading streams of raw bytes 

Read from a file using FileInputStream :- 

FileInputStream wrapped in BufferedInputStream increases efficiency of read operation.When an instance of FileInputStream(file) is created, a file descriptor object is created internally.File descriptor acts as an handle to the underlying machine-specific structure representing an open file. i.e: File descriptor binds stream with the file being used for read operation. Below sample code shows how FileInputStream wrapped with BufferedInputStream can be used for read operation. Complete sample Java program for read and write operation is here.
// With buffered input:- Read buffered input file and write to output file
  public static void readBinaryStreamEfficientWithBuffer(File outputFile,
      File inputFile) {
    int byteCoint;
    try {
      FileInputStream is = new FileInputStream(inputFile);
      // Buffered input stream and loop over buffered result
      BufferedInputStream bis = new BufferedInputStream(is);

      FileOutputStream os = new FileOutputStream(outputFile);
      BufferedOutputStream bos = new BufferedOutputStream(os);
      
      while ((byteCoint = bis.read()!= -1) {
        bos.write(byteCoint);
      }
      is.close();
      os.close();

    catch (IOException e) {
      e.printStackTrace();
    }
  }
What is significance of -1 in while loop:- On each read() operation(bis.read()), next byte of data from the input stream is read. The value byte is returned as an int in the range 0 to 255 and same is stored in byteCoint. If no byte is available because the end of the file/stream has been reached,
the value -1 is returned.( Add System.out.println(byteCoint) inside while loop and right after while loop and verify that integer between 0 to 255 and  -1 is be printed on console.)

Read from a file using FileReader :- 

For character streams, FileReader is used and it extends InputStreamReader(a bridge from byte streams to character streams).InputStreamReader reads streams and convert it into character(s) using character set converter. On each invocation of  read operation, byte(s) read is converted into characters So,efficiency of this read operation and conversion can be improved by buffering the InputStream using BufferedWriter.
Note :-  Since FileReader internally relies on InputStreamRedaer encoding and byte buffer. So, constructor of class FileReader assume that the default character encoding and the default byte-buffer size are appropriate.
Below sample code shows how FileReader wrapped with BufferedReader. Complete sample Java program for read and write operation is here.
// With buffered input:- Read input text file and write to output file
  public static void writeTextWithBufferedInput(File outputFile,
      File inputFile) {
    int readCount;
    try {
      FileReader fr = new FileReader(inputFile);
      br = new BufferedReader(fr);
      fw = new FileWriter(outputFile);
      BufferedWriter bwr = new BufferedWriter(fw);
      while ((readCount = br.read()!= -1) {
        bwr.write(readCount);
      }
      br.close();
      fw.close();
    catch (IOException e) {
      e.printStackTrace();
    }
  }

1 Comments

Previous Post Next Post