In the world of programming, handling input and output (I/O) is a fundamental task. Whether you're reading from or writing to files, network sockets, or other sources, understanding how to manage data flow efficiently is crucial. In this tutorial, we'll dive into Java's I/O streams, focusing on InputStream, OutputStream, and the differences between byte streams and character streams.
Java provides a robust framework for handling input and output operations through its I/O classes. These classes are organized into two main categories: byte streams and character streams. Byte streams handle raw binary data, while character streams handle text data encoded in specific character sets (like UTF-8). Understanding these concepts will help you build efficient and effective Java applications that interact with various input/output sources.
InputStream:
int read(): Reads the next byte of data from the input stream.int read(byte[] b): Reads up to b.length bytes of data from the input stream into an array of bytes.void close(): Closes the input stream and releases any system resources associated with it.OutputStream:
void write(int b): Writes the specified byte to this output stream.void write(byte[] b): Writes b.length bytes from the specified byte array to this output stream.void close(): Closes this output stream and releases any system resources associated with it.1import java.io.FileInputStream;2import java.io.FileOutputStream;3import java.io.InputStream;4import java.io.OutputStream;56public class StreamExample {7public static void main(String[] args) {8try (InputStream in = new FileInputStream("input.txt");9OutputStream out = new FileOutputStream("output.txt")) {1011int data;12while ((data = in.read()) != -1) {13out.write(data);14};15} catch (Exception e) {16e.printStackTrace();17}18}19}
Tip
Let's create a simple program that reads from a file, converts the content to uppercase, and writes it to another file using both byte streams and character streams.
1import java.io.FileInputStream;2import java.io.FileOutputStream;3import java.io.FileReader;4import java.io.FileWriter;5import java.io.InputStream;6import java.io.OutputStream;7import java.io.Reader;8import java.io.Writer;910public class UppercaseConverter {11public static void main(String[] args) {12// Using byte streams13try (InputStream in = new FileInputStream("input.txt");14OutputStream out = new FileOutputStream("output_byte_uppercase.txt")) {1516int data;17while ((data = in.read()) != -1) {18out.write(Character.toUpperCase((char) data));19}20} catch (Exception e) {21e.printStackTrace();22}2324// Using character streams25try (Reader reader = new FileReader("input.txt");26Writer writer = new FileWriter("output_char_uppercase.txt")) {2728int data;29while ((data = reader.read()) != -1) {30writer.write(Character.toUpperCase((char) data));31}32} catch (Exception e) {33e.printStackTrace();34}35}36}
Uppercase conversion using byte stream completed. Uppercase conversion using character stream completed.
Tip
| Concept | Description |
|---|---|
| InputStream | Abstract class for reading bytes from a source. |
| OutputStream | Abstract class for writing bytes to a destination. |
| Byte Streams | Handle raw binary data. |
| Character Streams | Handle text data encoded in specific character sets. |
In the next tutorial, we'll explore FileInputStream and FileOutputStream in more detail. These classes are essential for reading from and writing to files using byte streams. We'll cover their usage, methods, and practical examples to help you master file handling in Java. Stay tuned!