Next: Case study: Copying a file. Up: Files. Previous: The FileOutputStream class.


The FileInputStream class

Textbook: Section 14.3

The FileInputStream class is basically the opposite of the FileOutputStream class - a FileInputStream object is for reading bytes from a file.

Constructors

FileInputStream(File file) throws FileNotFoundException
Creates a FileInputStream object for the specified file. (It throws FileNotFoundException if the specified file doesn't exist or isn't readable.)

Reading methods

int read(byte[] b) throws IOException
Attempts to fill the array b with bytes from the file, returning the number of bytes successfully read, or returning -1 if the end of the file has already been reached.
void close() throws IOException
Closes the file for reading, making further reads from the object invalid.
Closing a FileInputStream file isn't as important as closing a FileOutputStream file, since there's no chance that data will be lost if the program aborts abnormally. Still, it's a good habit to get into - it saves you from problems with getting too many files open at once.


Next: Case study: Copying a file. Up: Files. Previous: The FileOutputStream class.