Java - IO - Connection (Stream and Channel)

Java Conceptuel Diagram

Java - IO - Connection (Stream and Channel)


I/O - Stream in Java.

In order to perform I/O operations (for example reading or writing), you need to perform a connection.

In Java, this connection are modelled through:

They are classes (and methods) that opens connection to an entity such as:

  • a file (mostly),
  • a hardware device,
  • a network socket,
  • or a program component

that is capable of performing one or more distinct I/O operations, for example reading or writing.

A stream or channel can be seen as a sequential byte (read and write operations):

  • you open a (channel|connection) from an entity (mostly a file)
  • ask or write repeatedly a quantity of data (by byte of array)
  • transform the byte (mostly as characters)
  • close (release) the (entity|channel|connection) when a condition is met (End of File, Until some address)

I/O Method

Java Fileiomethods

Stream Operations

Streams may be:

  • Chained: Streams of the same direction (Input|Ouptut) can be chained to another by passing it to the constructor of some second stream.
  • Concatenated via a SequenceInputStream (A SequenceInputStream represents the logical concatenation of other input streams)
  • Piped Data is read from a (PipedInputStream|PipedReader) object by one thread and data is written to the corresponding (PipedOutputStream|PipedWriter) by some other thread.


All other stream types are built on the byte streams FileInputStream and FileOutputStream (reading/writing file one byte at a time) See Java - IO - Byte Stream


Java - IO - Byte Stream

Data Type


Java - IO - Character Stream

Java Primitive

  • DataInputStream: A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way.
  • DataOutputStream: A data output stream lets an application write primitive Java data types to an output stream in a portable way.

Java Object

  • ObjectInputStream: An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.
  • ObjectOutputStream: An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream.


Filtered Streams = Transformation or More Functionalities on Streams.

A Filter(Input|Output)Stream uses (Input|Output) stream as its (source|target) of data:

  • possibly transforming the data along the way
  • or providing additional functionality.

The (superclass|abstract class) are:

Standard Stream

Java - IO - Standard Streams

Management Operations


Closing a stream when it's no longer needed is very important.

The use of a finally block to guarantee that streams will be closed even if an error occurs and helps avoid serious resource leaks.


try {
    in = new FileInputStream(Parameters.FILE_PATH_READ);
    out = new FileOutputStream(Parameters.FILE_PATH_WRITE);
    int c;

    while ((c = != -1) {
} finally {
    if (in != null) {
    if (out != null) {

One stream can be chained to another by passing it to the constructor of some second stream. When this second stream is closed, then it automatically closes the original underlying stream as well.

If multiple streams are chained together, then closing the one which was the last to be constructed, and is thus at the highest level of abstraction, will automatically close all the underlying streams. So, one only has to call close on one stream in order to close an entire series of related streams.

You should close the outermost OutputStream or Writer you have created from the socket output stream.

When a stream is closed, the connection between the stream and the entity (mostly a file) is canceled. After you have closed a stream, you cannot perform any additional operations on it.

Documentation / Reference

Discover More
Antlr Idea Plugin
Antlr - Parse Tree (AST)

The tree parser is an AST that is created by the parser from a text input. with grun, see the -tree of -gui option of Example: Type your text and end with a End of File character (Ctrl+Z or...
Java Conceptuel Diagram
Java - IO (Input/Output) - (File System|File|Console)

in Java: Ie File System Operations on file and directory Input / Output (IO) Operations (Input/Output - Read/Write) with stream Java has modeled a file system and IO operations: until Java...
Java Conceptuel Diagram
Java - IO - Byte Stream

Byte I/O Operations (Stream) in java Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from this two class: input stream to read byte or...
Java Conceptuel Diagram
Java - Java IO

Java IO is the first IO model of Java. It provides for system input and output through data streams, serialization and the file system. See java/io/ With the advent of Java 7, the...
Java Conceptuel Diagram
Java XML - XStream (Xml, Json)

XStream is not a data binding tool. It is a serialization tool. The architecture of XStream consists of the four main components: Component Description Converters...

Share this page:
Follow us:
Task Runner