Java Reference ยท I/O and Files
InputStream and OutputStream
Reference for java.io.InputStream and OutputStream: read and write bytes, transferTo, readAllBytes, buffering, and the rules for closing streams.
InputStream and OutputStream are the abstract base classes for reading and writing bytes in Java. Files, sockets, process pipes, System.in and System.out, compressed data and in-memory arrays are all streams. Concrete subclasses supply the source or destination; wrappers such as BufferedInputStream or DataOutputStream add behavior on top.
Use streams for binary data (images, archives, network protocols). For text, wrap them in a Reader or Writer with an explicit charset, or use the Files helpers.
try (InputStream in = Files.newInputStream(src); OutputStream out = Files.newOutputStream(dst)) { in.transferTo(out); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public abstract class InputStream implements Closeable public abstract class OutputStream implements Closeable, Flushable
| InputStream member | Description |
|---|---|
abstract int read() |
One byte as 0..255, or -1 at end of stream. Blocks until data is available. |
int read(byte[] b), read(byte[] b, int off, int len) |
Reads up to len bytes; returns how many were read, or -1 at the end. May return fewer than requested. |
byte[] readAllBytes() |
Reads the rest of the stream (Java 9+). |
byte[] readNBytes(int len) / int readNBytes(byte[], int, int) |
Keeps reading until len bytes or the end (Java 9+/11+). |
long transferTo(OutputStream out) |
Copies everything that remains (Java 9+). |
long skip(long n) / void skipNBytes(long n) |
Skip bytes; skipNBytes (Java 12+) throws EOFException if too short. |
int available() |
Estimate of bytes readable without blocking. Not a length. |
mark, reset, markSupported |
Optional rewinding. |
| OutputStream member | Description |
|---|---|
abstract void write(int b) |
Writes the low 8 bits of b. |
write(byte[] b), write(byte[] b, int off, int len) |
Writes an array or a part of it. |
flush() |
Forces buffered bytes to the destination. |
close() |
Flushes, then releases the resource. |
Throws: IOException from nearly everything.
Usage notes
read()returns anint, so that-1can signal the end. Casting tobytebefore comparing breaks the end check, because byte0xFFis also-1as abyte.- Partial reads are normal.
read(buf)may fill only part of the array; always use the returned count.readNBytesorDataInputStream.readFullykeep reading until the request is met. - Buffer small reads and writes. Unbuffered file streams make a system call per call; wrap them in
BufferedInputStreamorBufferedOutputStream, or read in blocks of 8 KB or more. - Closing: try-with-resources closes streams in reverse order. Closing a wrapper closes the wrapped stream.
available()is not the file size; for a file useFiles.size.- Text:
new String(bytes, charset)orInputStreamReaderwith a charset. Never rely on the platform default. System.in/System.outare anInputStreamand aPrintStream.PrintStreamswallowsIOException, likePrintWriter.- Not thread-safe in general; do not share a stream between threads without your own locking.
- For low-level
transferTobetween channels, seeFileChannel.