I/O streams in Java

I/O streams

The I/O streams in Java is performed through sorted sequences of bytes (streams)

These can be input (InputStream) or output (OutputStream) independent objects from data

The classes of E/S are in the package java.io

The methods of OutputStream and InputStream throw the exception IOException for any failure related to the attempt to read/write

The class OutputStream

Abstract class from which derive the other output streams

It essentially offers the following methods:

  • public abstract void write(int b) throws IOException
  • public void write(byte b[]) throws IOException
  • public void write(byte b[], int off, int len) throws IOException
  • public void flush() throws IOException

    Force the write

  • public void close() throws IOException

There are several classes that are derived from OutputStream and redefine the method write to give you a different implementation:

  • FileOutputStream

    it redefines the method write to send bytes to a file

  • ByteArrayOutputStream
  • PipedOutputStream
  • Sun.net.TelnetOutputStream

FilterOutputStream

A output filter it is a OutputStream to which will be associated to it in its constructor another OutputStream (a decorator pattern)

The filter forwarded all of the information you receive to your OutputStream associated, after carrying out some kind of transformation in it

In this way each filter adds an additional functionality to the OutputStream that encapsulates

You can chain multiple filters to obtain multiple functionalities, combined

In the example the file has loaded data.dat for writing and using the method write have saved several integer values that are stored in the variable i

He adds & 0xFF at the end of the write stream to indicate within the file that a data has finished writing

Filter DataOutputStream

The methods of the class OutputStream only allow you to send bytes

Any other type must be broken down into a sequence of bytes before it can be written

Among the most prominent methods:

  • void writeBytes(String s) throws IOException
  • void writeBoolean(boolean v) throws IOException
  • void writeShort(int v) throws IOException
  • void writeChar(int v) throws IOException
  • void writeInt(int v) throws IOException
  • void writeLong(long v) throws IOException
  • void writeFloat(float v) throws IOException
  • void writeDouble(double v) throws IOException
  • void writeChars(Strings s) throws IOException

    Almost uns used, the filter is more used PrintStream

In the example, two filters have been used, the FileOutputStream seen above and the DataOutputStream seen now

The class DataOutputStream adds the functionality of being able to send all primitive types directly

So you can write in the file any primitive type and even attributes of an object, as long as they are primitive types, since you do not write the entire object

Filter PrintStream

The class PrintStream adds the functionality of being able to send all of the primitive types in text format

In the example, two filters have been used, the FileOutputStream seen above and the PrintStream seen now

Methods print and println are overloaded for all primitive types

System.out is of type PrintStream and that's why it is not necessary to reference it by using this filter

The most common way of use OutputStream will be combining several filters

The class InputStream

Abstract class from which derive the other input streams

It essentially offers the following methods:

  • public abstract int read() throws IOException
  • public int read(byte b[]) throws IOException
  • public int read(byte b[], int off, int len) throws IOException
  • public int available() throws IOException
  • public void close() throws IOException

There are several classes that are derived from InputStream and redifinen the method read to give you a different implementation:

  • FileInputStream

    it redefines the method read to read bytes from a file

  • ByteArrayInputStream
  • StringInputStream
  • SequenceInputStream
  • PipedInputStream
  • Sun.net.TelnetInputStream

FilterInputStream

An input filter is a InputStream to which will be associated to it in its constructor another InputStream

The filters return the information which they have read of his InputStream associated, after carrying out some kind of transformation in it

In this way each filter adds an additional functionality to the InputStream basic

You can chain multiple filters to obtain multiple functionalities, combined

In the example the file has loaded data.dat for reading and using the method read have read several integer values that are stored in the variable ch in the positions i + 1 and we've shown by screen

So we have been able to read the file that we write with the example of FileOutputStream

Filter DataInputStream

The methods of the class InputStream only allow to read bytes

Any other type should be read as a sequence of bytes, and to pick up the pieces before it can be used

It essentially offers the following methods:

  • readBoolean()
  • readChar()
  • readByte()
  • readShort()
  • readInt()
  • readLong()
  • readFloat()
  • readLong()
  • readFloat()
  • readDouble()
  • readLine()
  • readFully(byte[] b)

In the example, two filters have been used, the FileInputStream seen previously, and the DataInputStream seen now

The class DataInputStream adds the functionality of being able to receive all of the primitive types directly

That has allowed us to display primitive data contents of the file directly on the screen

So read from the file any primitive type and even attributes of an object, as long as they are primitive types, since it does not read the entire object

The most common way of use InputStream will be combining several filters