How to read file using FileInputStream in Java?

UPDATED: 30 June 2015
Java 1.6 or less
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ReadFileInputStreamExample {
    
    public static void main(String[] args) {
        /* Create object of File. */
        File objFile = new File("D:\\Readme.txt");

        /* Create object of FileInputStream */
        FileInputStream objFileInputStream = null;
        try {
            /**
             * A FileInputStream obtains input bytes from a file in a file system. What files
             * are available depends on the host environment.
             *
             * FileInputStream is meant for reading streams of raw bytes
             * such as image data. For reading streams of characters, consider using
             * FileReader.
             */
            objFileInputStream = new FileInputStream(objFile);
            
            /* Read content of File. */
            int byteOfData;
            while ((byteOfData = objFileInputStream.read()) != -1) {
                /* Print content of File. */
                System.out.print((char) byteOfData);
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            /* Close the FileInputStream */
            if (objFileInputStream != null) {
                try {
                    objFileInputStream.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}

Java 1.7 or above(try-with-resources)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ReadFileInputStreamExample {
    
    public static void main(String[] args) {
        /* Create object of File. */
        File objFile = new File("D:\\Readme.txt");
        
        try (
                /**
                 * A FileInputStream obtains input bytes from a file in a file system. What files
                 * are available depends on the host environment.
                 *
                 * FileInputStream is meant for reading streams of raw bytes
                 * such as image data. For reading streams of characters, consider using
                 * FileReader.
                 */
                FileInputStream objFileInputStream = new FileInputStream(objFile);
            ) {
            /* Read content of File. */
            int byteOfData;
            while ((byteOfData = objFileInputStream.read()) != -1) {
                /* Print content of File. */
                System.out.print((char) byteOfData);
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } 
    }
}

Other References:
What is try-with-resources in Java 7 or above?
How to read file in Java?
How to read/parse XML file in Java?
How to write file in Java?
How to append text to an existing file in Java?

0 comments :