Java I/O Byte Streams

Byte streams in Java are used for input and output of raw binary data. They handle data in bytes (8-bit binary data) and are useful for reading and writing binary files such as images, audio files, and other media files. The primary classes for byte streams are InputStream and OutputStream and their subclasses.

Table of Contents

  1. Introduction
  2. Byte Stream Classes
    • InputStream
    • OutputStream
  3. Examples
    • Reading Bytes from a File
    • Writing Bytes to a File
    • Copying a File
  4. Conclusion

Introduction

Byte streams are the most basic type of I/O in Java. They read and write one byte at a time and are primarily used for binary data. Unlike character streams, byte streams do not handle character encoding and decoding, making them suitable for raw data processing.

Byte Stream Classes

InputStream

The InputStream class is the superclass of all classes representing an input stream of bytes. It provides methods for reading bytes from a source.

Common InputStream subclasses:

  • FileInputStream
  • ByteArrayInputStream
  • BufferedInputStream

OutputStream

The OutputStream class is the superclass of all classes representing an output stream of bytes. It provides methods for writing bytes to a destination.

Common OutputStream subclasses:

  • FileOutputStream
  • ByteArrayOutputStream
  • BufferedOutputStream

Examples

Reading Bytes from a File

The following example demonstrates how to read bytes from a file using FileInputStream.

Example

import java.io.FileInputStream;
import java.io.IOException;

public class ReadBytesExample {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("input.txt")) {
            int byteData;
            while ((byteData = fis.read()) != -1) {
                System.out.print((char) byteData); // Cast to char to display the content
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Explanation:

  • FileInputStream is used to read bytes from a file named input.txt.
  • The read() method reads one byte at a time and returns -1 when the end of the file is reached.
  • The byte data is cast to char for display purposes.

Writing Bytes to a File

The following example demonstrates how to write bytes to a file using FileOutputStream.

Example

import java.io.FileOutputStream;
import java.io.IOException;

public class WriteBytesExample {
    public static void main(String[] args) {
        try (FileOutputStream fos = new FileOutputStream("output.txt")) {
            String content = "Hello, World!";
            fos.write(content.getBytes()); // Convert string to bytes
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Explanation:

  • FileOutputStream is used to write bytes to a file named output.txt.
  • The write() method writes the byte array to the file.
  • The getBytes() method is used to convert the String content to a byte array.

Copying a File

The following example demonstrates how to copy a file using FileInputStream and FileOutputStream.

Example

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFileExample {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("source.txt");
             FileOutputStream fos = new FileOutputStream("destination.txt")) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Explanation:

  • FileInputStream reads bytes from source.txt.
  • FileOutputStream writes bytes to destination.txt.
  • A buffer of 1024 bytes is used to read and write chunks of data, improving performance.
  • The read() method reads up to the length of the buffer and returns the number of bytes read.
  • The write() method writes the specified number of bytes from the buffer.

Conclusion

Byte streams in Java provide a way to handle I/O of raw binary data. They are essential for reading and writing binary files and are the foundation for more advanced I/O operations. By understanding how to use InputStream and OutputStream classes, you can efficiently manage binary data in your Java applications. The examples provided demonstrate basic usage, including reading from a file, writing to a file, and copying a file.

Comments