Java I/O Streams

h‮sptt‬://www.theitroad.com

Java I/O (Input/Output) streams are a set of classes and methods used for reading and writing data to and from various sources in Java. Streams in Java provide a flexible and efficient way of working with data, allowing you to read or write data in small chunks or large amounts.

There are two types of I/O streams in Java: byte streams and character streams. Byte streams are used for reading and writing raw bytes of data, while character streams are used for reading and writing text data in the form of characters.

Here's an example of how to use byte streams to read and write data to a file in Java:

import java.io.*;

public class FileStreamExample {
    public static void main(String[] args) {
        try {
            // Create an output stream to write data to a file
            OutputStream os = new FileOutputStream("file.txt");

            // Write data to the file
            byte[] data = "Hello, world!".getBytes();
            os.write(data);

            // Close the output stream
            os.close();

            // Create an input stream to read data from the file
            InputStream is = new FileInputStream("file.txt");

            // Read data from the file
            byte[] buffer = new byte[1024];
            int bytesRead = is.read(buffer);
            String content = new String(buffer, 0, bytesRead);
            System.out.println(content);

            // Close the input stream
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we first create an output stream os to write data to a file named file.txt. We write the text "Hello, world!" to the file using the write method, which writes a byte array to the output stream. Finally, we close the output stream using the close method.

Next, we create an input stream is to read data from the file. We read the data from the file into a buffer using the read method, which reads a block of data from the input stream into an array of bytes. We then convert the bytes to a string and print it to the console. Finally, we close the input stream using the close method.

Java also provides character streams, which are used for reading and writing text data. Here's an example of how to use a character stream to read data from a file:

import java.io.*;

public class CharStreamExample {
    public static void main(String[] args) {
        try {
            // Create a reader to read text data from a file
            Reader reader = new FileReader("file.txt");

            // Read text data from the file
            char[] buffer = new char[1024];
            int charsRead = reader.read(buffer);
            String content = new String(buffer, 0, charsRead);
            System.out.println(content);

            // Close the reader
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we create a Reader object to read text data from a file named file.txt. We read the data from the file into a buffer using the read method, which reads a block of text data from the input stream into an array of characters. We then convert the characters to a string and print it to the console. Finally, we close the reader using the close method.

Java I/O streams provide a powerful and flexible way of working with data in Java. By using streams, you can read and write data to and from various sources, including files, network connections, and memory buffers.