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.

  1. FileInputStream(File file): Creates a FileInputStream object by opening a connection to an actual file by using specified file object.
  2. 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.
  3. 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.

  1. 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.
  2. 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.
  3. finalize(): This method ensures that the close() method of this file input stream is called when there are no more reference to it.
  4. 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.
  5. getFD(): This method returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.
  6. 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.
  7. 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.
  8. 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.
  9. 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

 

 

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.

 

 

 

 

Sushil Singh

My name is Sushil Kumar Singh. I’m the founder, designer and editor of this blog. I would love to hear your thoughts and opinions on my articles directly. You can write to me at sushilsingh@learningsolo.com. I’ll try to respond as soon as possible. Alternatively, you can connect with me directly on linkedin or github or Facebook.

Leave a Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of