Java Methods of FileInputStream

www.igift‮di‬ea.com

The FileInputStream class in Java provides several methods for reading data from a file as a stream of bytes. Some of the commonly used methods of the FileInputStream class are:

  1. read() - Reads a single byte of data from the input stream and returns it as an integer value. Returns -1 if the end of the stream has been reached.

  2. read(byte[] b) - Reads a sequence of bytes from the input stream and stores them in the byte array b. Returns the number of bytes read, or -1 if the end of the stream has been reached.

  3. read(byte[] b, int off, int len) - Reads up to len bytes of data from the input stream and stores them in the byte array b, starting at the specified offset off. Returns the number of bytes read, or -1 if the end of the stream has been reached.

  4. skip(long n) - Skips over and discards n bytes of data from the input stream. Returns the actual number of bytes skipped.

  5. available() - Returns an estimate of the number of bytes that can be read from the input stream without blocking.

  6. close() - Closes the input stream, releasing any system resources associated with the stream.

It's important to note that the behavior of these methods can vary depending on the specific implementation of the FileInputStream class. For example, the FileInputStream class may buffer data internally to improve performance, and the behavior of the read method may be affected by this buffering.

Overall, the FileInputStream class provides a flexible and efficient way of reading data from a file in Java, and the various methods it provides can be used to read data in a variety of different ways depending on your specific needs. By using the various methods of the FileInputStream class, you can read data from files in a variety of formats, including text files, binary files, and more.