Java FileInputStream
Java FileInputStream
- Java FileInputStream class is a part of java.io package.
- FileInputStream obtains input bytes from a file in a file system.
- FileInputStream is used for reading streams of raw bytes such as image data.
- FileInputStream is a subclass of InputStream class.
FileInputStream Class Hierarchy
FileInputStream Constructors
FileInputStream provides many methods for reading bytes from a file and we can create an instance of FileInputStream
 using below constructors.
FileInputStream(File file)
: Creates a FileInputStream object by opening a connection to an actual file by using specified file object.FileInputStream(FileDescriptor fdObj)
: Creates a FileInputStream object by using the specified file descriptor fdObj, which represents an existing connection to an actual file in the file system.FileInputStream(String name)
: Creates a FileInputStream object by opening a connection to an actual file named by the specified name which represents path of the file.
FileInputStream Methods
Let’s have a look at the below methods of FileInputStream class.
available()
: This method returns an estimate of the number of remaining bytes that can be read from input stream without blocking by the next invocation of method for this input stream.close()
: This method closes this file input stream and release any system resources associated with the stream. If this stream has an associated channel then channel is closed as well.finalize()
: This method ensures that the close() method of this file input stream is called when there are no more reference to it.getChannel()
: This method returns the unique FileChannel object associated with this file input stream. The initial position of the returned channel will be equal to the number of bytes read from the file and reading bytes from this stream will increment the channel’s position.getFD()
: This method returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.read()
: This method reads a byte of data from this input stream and returns the next byte of data or -1 if the end of file is reached.read(byte[] b)
: This method reads byte of data up to the length of specified array(b.length) from this input stream into specified array of bytes and returns the total number of bytes read into the buffer or -1 if the end of file is reached.read(byte[] b, int off, int len)
: This method reads bytes of data up to specified len from this input stream into specified array of bytes. If specified len is not zero, the method blocks until some input are available; otherwise no bytes are read and 0 is returned. It returns number of bytes read into the buffer or -1 if the end of file is reached.skip(long n)
: This method skips over and discards specified n bytes of data from the input stream and returns the actual number of bytes skipped. It throws an IOException if the specified n is negative.
Java FileInputStream Examples
Let’s look at some example for FileInputStream class.
Read File using FileInputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class FileReader { public static void main(String[] args) { File file = null; FileInputStream fis = null; try { file = new File("D:/data/file.txt"); fis = new FileInputStream(file); System.out.println("Available bytes in file: "+fis.available()); int line; while ((line=fis.read()) != -1) { System.out.print((char)line); } } catch (Exception e) { e.printStackTrace(); }finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } } |
Java 7 update
Java 7 provides try with resource which is a try statement that declares one or more resource. A resource is an object that must be closed after program is finished with it. The try with resource statement ensures that each resource is closed at the end of the statement.
Let’s have a look at the below example program using try with resources.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class FileReaderWithResource { public static void main(String[] args) { File file = new File("D:/data/file.txt"); try (FileInputStream fis = new FileInputStream(file)){ System.out.println("Available bytes in file: "+fis.available()); int line; while ((line=fis.read()) != -1) { System.out.print((char)line); } } catch (Exception e) { e.printStackTrace(); } } } |