Menu
Topics Index
...
`


Input/Output: Exploring java.io > Byte Streams >
Siva Nookala - 06 Oct 2016
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 Java FileReader.

FileInputStream Constructors:
ConstructorDescription
FileInputStream(File file)Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.
FileInputStream(FileDescriptor fdObj)Creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.
FileInputStream(String name)Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.


FileInputStream Methods:
MethodDescription
int available()Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
void close()Closes this file input stream and releases any system resources associated with the stream.
protected void finalize()Ensures that the close method of this file input stream is called when there are no more references to it.
FileChannel getChannel()Returns the unique FileChannel object associated with this file input stream.
FileDescriptor getFD()Returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.
int read()Reads a byte of data from this input stream.
int read(byte[] b)Reads up to b.length bytes of data from this input stream into an array of byte.
int read(byte[] b, int off, int len)Reads up to len bytes of data from this input stream into an array of bytes, starting at offset off in the destination array b.
long skip(long n)Skips over and discards n bytes of data from the input stream.


Example Program:
/* This is the content of file : MeritCampus.txt

This is an example for FileInputStream.

*/

import java.io.FileInputStream;
import java.io.File;
import java.awt.BorderLayout;
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Test {

    public static void main(String[] args) {

        File file = new File("MeritCampus.txt");
        try (FileInputStream fis1 = new FileInputStream(file)) {
            //display available bytes
            System.out.println("Total file size to read (in bytes) : "+ fis1.available());
            //skip bytes from file input stream
            fis1.skip(5);
            int content;
            while ((content = fis1.read()) != -1) {
                //to convert into char and display it
                System.out.print((char) content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        Image image=null;

        try (FileInputStream fis2 = new FileInputStream("image.jpg")) {
            image = ImageIO.read(fis2);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //to display the image read
        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ImageIcon(image));
        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
}

Output:
Total file size to read (in bytes) : 39
is an example for FileInputStream.
//the following image is displayed in a window

Programmer_definition
Description:
The input bytes of the file MeritCampus.txt is obtained by the FileInputStream and the total size of the file is displayed. Five bytes of the file is skipped and the remaining content is read and printed. The second FileInputStream obtains an image and it is displayed with the help of Java Swing in a window. Note that the full path is to be given if the file or image is not located in the same folder as the java file on the system and this is a JDK7 example, where the new “try resource close” method to handle the file easily is used.

Things to try:
  • Try to read the data into an array using read(byte[] b) method instead of just reading data and printing them.
  • Try to read the data into an array starting at the offset using read(byte[] b, int off, int len) method instead of just reading data and printing them.
  • Try displaying different images in different formats by obtaining input bytes using FileInputStream.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.