Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.io

Class FileInputStream

java.lang.Object
|
+--java.io.InputStream
   |
   +--java.io.FileInputStream


public class FileInputStream

extends InputStream

This class is a stream that reads its bytes from a file.

Author:

Constructor Summary

FileInputStream(java.lang.String name)

This method initializes a FileInputStream to read from the specified named file.
FileInputStream(java.io.File file)

This method initializes a FileInputStream to read from the specified File object.
FileInputStream(java.io.FileDescriptor fd)

This method initializes a FileInputStream to read from the specified FileDescriptor object.

Method Summary

intavailable()

This method returns the number of bytes that can be read from this stream before a read can block.
synchronized voidclose()

This method closes the stream.
voidfinalize()

This method ensures that the underlying file is closed when this object is garbage collected.
java.nio.channels.FileChannelgetChannel()

java.io.FileDescriptorgetFD()

This method returns a FileDescriptor object representing the underlying native file handle of the file this stream is reading from
synchronized intread()

This method reads an unsigned byte from the input stream and returns it as an int in the range of 0-255.
intread(byte[] buf)

This method reads bytes from a stream and stores them into a caller supplied buffer.
synchronized intread(byte[] buf, int offset, int len)

This method read bytes from a stream and stores them into a caller supplied buffer.
synchronized longskip(long num_bytes)

This method skips the specified number of bytes in the stream.

Constructor Details

FileInputStream

public FileInputStream(java.io.File file)

This method initializes a FileInputStream to read from the specified File object. A security check is first made to determine whether or not access to this file is allowed. This is done by calling the checkRead() method of the SecurityManager (if one exists) with the name of this file. An exception is thrown if reading is not allowed. If the file does not exist, an exception is also thrown.

Parameters:

Throws:


FileInputStream

public FileInputStream(java.io.FileDescriptor fd)

This method initializes a FileInputStream to read from the specified FileDescriptor object. A security check is first made to determine whether or not access to this file is allowed. This is done by calling the checkRead() method of the SecurityManager (if one exists) with the specified FileDescriptor An exception is thrown if reading is not allowed.

Parameters:

Throws:


FileInputStream

public FileInputStream(java.lang.String name)

This method initializes a FileInputStream to read from the specified named file. A security check is first made to determine whether or not access to this file is allowed. This is done by calling the checkRead() method of the SecurityManager (if one exists) with the name of this file. An exception is thrown if reading is not allowed. If the file does not exist, an exception is also thrown.

Parameters:

Throws:


Method Details

available

public int available()

This method returns the number of bytes that can be read from this stream before a read can block. A return of 0 indicates that blocking might (or might not) occur on the very next read attempt.

This method returns the number of unread bytes remaining in the file if the descriptor being read from is an actual file. If this method is reading from a ''special'' file such a the standard input, this method will return the appropriate value for the stream being read.

Be aware that reads on plain files that do not reside locally might possibly block even if this method says they should not. For example, a remote server might crash, preventing an NFS mounted file from being read.

Returns:

Throws:


close

public synchronized void close()

This method closes the stream. Any futher attempts to read from the stream will likely generate an IOException since the underlying file will be closed.

Throws:


finalize

protected void finalize()

This method ensures that the underlying file is closed when this object is garbage collected. Since there is a system dependent limit on how many files a single process can have open, it is a good idea to close streams when they are no longer needed rather than waiting for garbage collectio to automatically close them.

Throws:


getChannel

public FileChannel getChannel()


getFD

public final FileDescriptor getFD()

This method returns a FileDescriptor object representing the underlying native file handle of the file this stream is reading from

Returns:

Throws:


read

public synchronized int read()

This method reads an unsigned byte from the input stream and returns it as an int in the range of 0-255. This method also will return -1 if the end of the stream has been reached.

This method will block until the byte can be read.

Returns:

Throws:


read

public int read(byte[] buf)

This method reads bytes from a stream and stores them into a caller supplied buffer. This method attempts to completely fill the buffer, but can return before doing so. The actual number of bytes read is returned as an int. A -1 is returned to indicate the end of the stream.

This method will block until some data can be read.

This method operates by calling an overloaded read method like so: read(buf, 0, buf.length)

Parameters:

Returns:

Throws:


read

public synchronized int read(byte[] buf, int offset, int len)

This method read bytes from a stream and stores them into a caller supplied buffer. It starts storing the data at index offset into the buffer and attempts to read len bytes. This method can return before reading the number of bytes requested. The actual number of bytes read is returned as an int. A -1 is returned to indicate the end of the stream.

This method will block until some data can be read.

Parameters:

Returns:

Throws:


skip

public synchronized long skip(long num_bytes)

This method skips the specified number of bytes in the stream. It returns the actual number of bytes skipped, which may be less than the requested amount.

This method reads and discards bytes into a 256 byte array until the specified number of bytes were skipped or until either the end of stream is reached or a read attempt returns a short count. Subclasses can override this metho to provide a more efficient implementation where one exists.

Parameters:

Returns:

Throws: