Java's Input/Output (I/O) streams are an essential concept for handling data input and output in applications. Let's start with the basics and then gradually move to more advanced concepts.
A stream is a sequence of data elements made available over time. In Java, streams are used to read data from input devices or write data to output devices.
Key Points:
java.io
package to represent these streams through predefined classes.Java has two primary categories of streams:
Byte-Oriented Streams
InputStream: Reads binary data (byte-by-byte) from a source (e.g., file, keyboard).
OutputStream: Writes binary data (byte-by-byte) to a destination (e.g., file, monitor).
Character-Oriented Streams
Reader: Reads text (character-by-character) from a source.
Writer: Writes text (character-by-character) to a destination.
Streams are like a pipeline through which data flows between the input devices and output devices. Data is read from the input stream, processed in the application, and then written to the output stream.
Flow of I/O Streams:
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteStreamWriteExample {
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream("output.txt")) {
String message = "Hello, Java I/O!";
fos.write(message.getBytes()); // Convert string to bytes and write to the file
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
FileOutputStream
: Writes bytes to a file.fos.write()
: Converts the string to a byte array and writes it to the file.xxxxxxxxxx
import java.io.FileInputStream;
import java.io.IOException;
public class ByteStreamExample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("example.txt")) {
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data); // Converting byte to character
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
FileInputStream
: This class is used to read bytes from a file.fis.read()
: Reads a single byte at a time from the file. If there’s no more data, it returns -1
.System.out.print((char) data)
: Converts the byte to a character and prints it.xxxxxxxxxx
import java.io.FileReader;
import java.io.IOException;
public class CharacterStreamReadExample {
public static void main(String[] args) {
try (FileReader fr = new FileReader("example.txt")) {
int data;
while ((data = fr.read()) != -1) {
System.out.print((char) data); // Reading character-by-character
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
FileReader
: Reads characters from a file.fr.read()
: Reads one character at a time and converts it to a character using (char)
.xxxxxxxxxx
import java.io.FileWriter;
import java.io.IOException;
public class CharacterStreamWriteExample {
public static void main(String[] args) {
try (FileWriter fw = new FileWriter("output.txt")) {
fw.write("Writing characters using FileWriter!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
FileWriter
: Writes characters to a file.fw.write()
: Writes a string to the file, character-by-character.