Tuesday 19 June 2012

FileInputStream vs FileReader in Java

Java has Two types of streams- Byte & Characters. Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from java.io.InputStream  and java.io.OutputStream.The character streams can be used to perform input and output of text files.Character streams are implemented by the java.io.Reader and java.io.Writer classes and their subclasses.

FileInputStream is meant for reading streams of raw bytes such as image data.

You can use FileReader to read streams of characters like .csv and .txt files as both file types are files that contain characters.

Unless you are working with binary data, such as image and sound files, you should use readers and writers (character streams) to read and write information for the following reasons:

  • They can handle any character in the Unicode character set (while the byte streams are limited to ISO-Latin-1 8-bit bytes).

  • They are easier to internationalize because they are not dependent upon a specific character encoding.

  • They use buffering techniques internally and are therefore potentially much more efficient than byte streams.



Example

 1) Copy an Image file using FileInputStream and FileOutputStream

The following code reads an image file mca1.png using FileInputStream and copies it using FileOutputStream.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyImage {

    /**
     * @param args
     * @throws IOException
     **/
    public static void main(String[] args) {

        FileInputStream fin = null;
        FileOutputStream fout = null;

        try {
            fin = new FileInputStream("/home/radhakrishnan/Desktop/mca1.png");
            fout = new FileOutputStream(
                    "/home/radhakrishnan/Desktop/mca1_copy.png");
            int ch;
            while ((ch = fin.read()) != -1) {
                fout.write(ch);
            }
            System.out.println("Image copy completed");
        } catch (IOException e) {
            System.out.println(e);
        } finally {
            try {
                fin.close();
                fout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}


The output is

Image copy completed

2) Copy a text file using FileReader and FileWriter 

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyText {

    /**
     * @param args
     * @throws IOException
     **/
    public static void main(String[] args) {

        FileReader fread = null;
        FileWriter fwrite = null;

        try {
            fread = new FileReader("/home/radhakrishnan/Desktop/t1.txt");
            fwrite = new FileWriter("/home/radhakrishnan/Desktop/t2.txt");
            int ch;
            while ((ch = fread.read()) != -1) {
                fwrite.write(ch);
                System.out.print((char) ch);
            }
            System.out.println("text copy completed");
        } catch (IOException e) {
            System.out.println(e);
        } finally {
            try {
                fread.close();
                fwrite.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}


The output is

Hello welcome to Java I/O programming
text copy completed

No comments:

Post a Comment