Skip to content
Tutorials Center
My learning

Topics

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.

Java
try (InputStream in = Files.newInputStream(src);
     OutputStream out = Files.newOutputStream(dst)) {
    in.transferTo(out);
}

Try it

Java Reading bytes from an InputStream

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.

Java Copying with transferTo and a custom stream

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

Java
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 an int, so that -1 can signal the end. Casting to byte before comparing breaks the end check, because byte 0xFF is also -1 as a byte.
  • Partial reads are normal. read(buf) may fill only part of the array; always use the returned count. readNBytes or DataInputStream.readFully keep reading until the request is met.
  • Buffer small reads and writes. Unbuffered file streams make a system call per call; wrap them in BufferedInputStream or BufferedOutputStream, 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 use Files.size.
  • Text: new String(bytes, charset) or InputStreamReader with a charset. Never rely on the platform default.
  • System.in / System.out are an InputStream and a PrintStream. PrintStream swallows IOException, like PrintWriter.
  • Not thread-safe in general; do not share a stream between threads without your own locking.
  • For low-level transferTo between channels, see FileChannel.