Java FileInputStream Reference
›Short versionOpen if you only have a minute.
- FileInputStream reads bytes. It does not decode characters or split lines.
- read() returns 0 through 255, or -1 at the end of the file. The return type is int so -1 can exist.
- read(byte[]) returns how many bytes were stored, or -1 at the end. The buffer is not always full.
- Casting the int from read() to byte before you test for -1 makes a real 255 look like end of file.
- available() is an estimate of bytes that can be read without blocking. It is not the file length.
- Close the stream with try-with-resources. A read after close throws IOException.
FileInputStream reads bytes from a file. It is appropriate for binary data or as the low-level source wrapped by higher-level readers and buffers.
Methods
| API | Parameters | Returns | Behavior | Watch for |
|---|---|---|---|---|
| new FileInputStream(File/String) | File object or path string. | Open FileInputStream. | Opens the file for byte reading. | Throws FileNotFoundException if missing, inaccessible, or a directory. |
| read() | None. | 0-255 byte value as int, or -1 at EOF. | Reads one byte. | Casting to byte before checking -1 loses EOF signal. |
| read(byte[] buffer) | Destination byte array. | Bytes read, or -1 at EOF. | Reads up to buffer.length bytes. | Do not assume the whole buffer was filled. |
| skip(long n) | Maximum bytes to skip. | Actual bytes skipped. | Moves forward in the stream. | May skip fewer bytes than requested. |
| available() | None. | Estimated bytes readable without blocking. | Provides a hint, not total file size. | Do not use it as a loop condition for all input. |
| close() | None. | void. | Releases the file descriptor. | Further reads after close throw IOException. |
Which call fits
| Option | Best for | Tradeoff |
|---|---|---|
| FileInputStream | Binary files such as images, PDFs, and serialized bytes. | No character decoding or line handling. |
| FileReader | Simple character input with default charset. | Default charset can make programs less portable. |
| BufferedInputStream | Reducing system calls around byte streams. | Adds another wrapper to manage. |
Examples
Read bytes in chunks
java
try (FileInputStream in = new FileInputStream("photo.jpg")) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
System.out.println("Read " + bytesRead + " bytes");
}
}Copy to an output stream
java
try (FileInputStream in = new FileInputStream("in.bin");
FileOutputStream out = new FileOutputStream("out.bin")) {
byte[] buffer = new byte[8192];
int count;
while ((count = in.read(buffer)) != -1) {
out.write(buffer, 0, count);
}
}The loop is while ((byte) in.read() != -1). The file contains the byte 255 and then more data. What happens?
The loop stops at 255, before the file ends. (byte) 255 is -1, the same value read() uses for end of file, so the condition cannot tell them apart. Store the int, stop when it is -1, and only then narrow it: int b; while ((b = in.read()) != -1).