- Syntax:
java.lang.Object
java.io.InputStream - This abstract class is the superclass of all classes representing an input stream of bytes.
- Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input.
- It has one constructor and seven methods.
- Constructor: public InputStream()
- Methods:
- 1. read:
- Syntax:
- public abstract int read() throws IOException
- public int read(byte[] b) throws IOException
- public int read(byte[] b, int off, int len) throws IOException
- Use:
- Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.
- The first byte read is stored into element b[0], the next one into b[1], and so on.
- The first byte read is stored into element b[off], the next one into b[off+1], and so on.
- Parameters:
- b - the buffer into which the data is read.
- off - the start offset in array b at which the data is written.
- len - the maximum number of bytes to read.
- Returns: the next byte of data, or -1 if the end of the stream is reached.
- Throws:
- IOException - If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.
- NullPointerException - If b is null.
- IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off
- 2. available:
- Syntax: public int available() throws IOException
- Use: Returns an estimate of the number of 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.
- Returns: an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream.
- Throws: IOException - if an I/O error occurs.
- 3. close:
- Syntax: public void close() throws IOException
- Use: Closes this input stream and releases any system resources associated with the stream. The close method of InputStream does nothing.
- Throws: IOException - if an I/O error occurs.
- 4. mark:
- Syntax: public void mark(int readlimit)
- Use: Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes. The mark method of InputStream does nothing.
- Parameters: readlimit - the maximum limit of bytes that can be read before the mark position becomes invalid.
- 5. markSupported:
- Syntax: public boolean markSupported()
- Use: Tests if this input stream supports the mark and reset methods. Whether or not mark and reset are supported is an invariant property of a particular input stream instance. The markSupported method of InputStream returns false.
- Returns: true if this stream instance supports the mark and reset methods; false otherwise.
- 6. reset:
- Syntax: public void reset() throws IOException
- Use: Repositions this stream to the position at the time the mark method was last called on this input stream.
- Throws: IOException - if this stream has not been marked or if the mark has been invalidated.
- 7. skip:
- Syntax: public long skip(long n) throws IOException
- Use: Skips over and discards n bytes of data from this input stream.
- Parameters: n - the number of bytes to be skipped.
- Returns: the actual number of bytes skipped.
- Throws: IOException - if the stream does not support seek, or if some other I/O error occurs.
FileInputStream Class:
- Syntax:
java.io.InputStream
java.io.FileInputStream
- A FileInputStream obtains input bytes from a file in a file system.
- FileInputStream is meant for reading streams of raw bytes such as image data.
- It has three constructors and seven methods.
- Constructor:
- Syntax:
- public FileInputStream(File file) throws FileNotFoundException
- public FileInputStream(FileDescriptor fdObj)
- public FileInputStream(String name) throws FileNotFoundException
- Use:
- Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.
- Creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.
- Parameters:
- file - the file to be opened for reading.
- fdObj - the file descriptor to be opened for reading.
- Throws:
- FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
- SecurityException - if a security manager exists and its checked method denies read access to the file.
- Methods:
- 1. available:
- It is an overridden method from the InputStream class.
- Note: for more details: See InputStream Class (Methods: 02)
- 2. close:
- It is an overridden method from the InputStream class.
- Note: for more details: See InputStream Class (Methods: 03)
- 3. finalize:
- Syntax: protected void finalize() throws IOException
- Use: Ensures that the close method of this file input stream is called when there are no more references to it.
- Overrides: finalize in class Object
- Throws: IOException - if an I/O error occurs.
- 4. getChannel:
- Syntax: public FileChannel getChannel()
- Use: Returns the unique FileChannel object associated with this file input stream.
- Returns: the file channel associated with this file input stream
- 5. getFD
- Syntax: public final FileDescriptor getFD() throws IOException
- Use: Returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.
- Returns: the file descriptor object associated with this stream.
- Throws: IOException - if an I/O error occurs.
- 6. read
- It is an overridden method from the InputStream class.
- Note: for more details: See InputStream Class (Methods: 01)
- 7. skip
- It is an overridden method from the InputStream class.
- Note: for more details: See InputStream Class (Methods: 07)
Example (Read File):
- Step 01: Create a .txt file.
- Step 02: Write a JAVA program.
- Step 03: Enjoy the Output.
Tags:
Java