Course Content
Core Java
About Lesson

Java I/O (Input/Output) Streams provide a way to read and write data, either byte by byte or character by character.

The streams are divided into two categories:

  1. Byte Streams
  2. Character Streams

 

ByteStreams

ByteStreams in Java are used for handling the input and output of raw binary data. They operate on 8-bit bytes and are suitable for all kinds of data, including text, images, audio, and video files.

All byte stream classes are descended from InputStream and OutputStream.

InputStream

This abstract class is the superclass of all classes representing an input stream of bytes. A subclass of InputStream must always provide a method read() that returns the next byte of input.

Examples of common ByteStream (subclasses of InputStream) classes

  1. FileInputStream: A FileInputStream obtains input bytes from a file in a file system. It is meant for reading streams of raw bytes such as image data.
  2. BufferedInputStream: used to read information from stream. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time.
  3. DataInputStream: Read or write primitive Java data types in a machine-independent format.

OutputStream

This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some target. A subclass of OutputStream must always provide at least a method that writes write(int n)one byte of output.

Examples of common ByteStream (subclasses of OutputStream) classes

  1. FileOutputStream: A file output stream is an output stream for writing data to a File . FileOutputStream is meant for writing streams of raw bytes such as image data.  Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.
  2. BufferedOutputStream: The class implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written. During the write operation, the bytes are written to the internal buffer instead of the disk. Once the buffer is filled or the stream is closed, the whole buffer is written to the disk.
  3. DataOuputStream: A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.

CharacterStreams

The java.io package provides CharacterStream classes to overcome the limitations of ByteStream classes, which can only handle the 8-bit bytes and is not compatible to work directly with the Unicode characters. CharacterStream classes are used to work with 16-bit Unicode characters. They can perform operations on characters, char arrays and Strings. Character stream automatically allows us to read/write data character by character.

All Character Stream classes are derived from Reader and Writer classes.

Reader

The Reader class in Java is part of the java.io package and serves as an abstract base class for reading character streams. It provides basic functionality and methods to read characters, arrays, and lines of text.

The only methods that its subclasses must implement are read(char[], int, int) and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.

Examples of common CharacterStreams classes (subclasses of Reader class)

  1. BufferedReader: Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
  2. InputStreamReader: It is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform’s default charset may be accepted.
  3. FileReader: For reading character files.
  4. CharArrayReader: to read characters from a character array as if it were an input stream.
  5. StringReader: A character stream whose source is a string. It takes an input string and changes it into character stream.

Writer

The Writer class in Java is part of the java.io package and serves as an abstract base class for writing character streams. It provides basic functionality and methods to write characters, arrays, and strings to various outputs like files, network sockets, and other I/O sources.

The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.

Examples of common CharacterStreams classes (subclasses of Writer class)

  1. BufferedWriter: It writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.
  2. OutputStreamWriter: It is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset. Each invocation of a write() method causes the encoding converter to be invoked on the given character(s). The resulting bytes are accumulated in a buffer before being written to the underlying output stream.
  3. FileWriter: It is used to write data in character form to file.
  4. CharArrayWriter: This class implements a character buffer that can be used as an Writer. The buffer automatically grows when data is written to the stream. The data can be retrieved using toCharArray() and toString().
  5. StringWriter: A character stream that collects its output in a string buffer, which can then be used to construct a string. Closing a StringWriter has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

FileInputStream

import java.io.File;
import java.io.FileInputStream;
import java.io.FileDescriptor;
import java.io.IOException;

public class FileInputStreamExample {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;

        try {
            // Initialize FileInputStream with a file
            File file = new File("example.txt");
            fileInputStream = new FileInputStream(file);

            // Read a single byte
            int singleByte = fileInputStream.read();
            System.out.println("Read single byte: " + (char) singleByte);

            // Read into a byte array
            byte[] buffer = new byte[10];
            int bytesRead = fileInputStream.read(buffer);
            System.out.println("Read bytes into buffer: " + new String(buffer, 0, bytesRead));

            // Skip bytes
            long bytesSkipped = fileInputStream.skip(2);
            System.out.println("Skipped bytes: " + bytesSkipped);

            // Check available bytes
            int availableBytes = fileInputStream.available();
            System.out.println("Available bytes: " + availableBytes);

            // Read remaining bytes into a buffer
            bytesRead = fileInputStream.read(buffer, 0, buffer.length);
            System.out.println("Read remaining bytes into buffer: " + new String(buffer, 0, bytesRead));

            // Get FileDescriptor
            FileDescriptor fd = fileInputStream.getFD();
            System.out.println("FileDescriptor: " + fd);

            // Get FileChannel
            java.nio.channels.FileChannel fileChannel = fileInputStream.getChannel();
            System.out.println("FileChannel: " + fileChannel);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // Close the file input stream
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                    System.out.println("Stream closed.");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Scroll to Top