One of the important aspects of developing any application is handling input and output. Java input/output streams allow data to be read from or written to files or other sources.
Table of Contents
Types of Java I/O Streams:
In Java, there are two types of I/O streams:
- Byte streams
- Character streams
Byte Streams:
Byte streams are used to read and write binary data, such as images, audio, and video files. Java provides several classes for working with byte streams, including
- InputStream, InputStream is an abstract class that is the superclass of all classes that represent input streams of bytes.
- OutputStream, OutputStream is also an abstract class that is the superclass of all classes that represent output streams of bytes.
- FileInputStream /FileOutputStream, FileInputStream and FileOutputStream are used to read and write data from and to files, respectively.
- BufferedInputStream /BufferedOutputStream BufferedInputStream and BufferedOutputStream are used to improve the performance of reading and writing data from and to files.
import java.io.*;
public class ByteStreamsExample {
public static void main(String[] args) {
try {
FileInputStream in = new FileInputStream("input.txt");
FileOutputStream out = new FileOutputStream("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
out.close();
} catch(IOException e) {
System.out.println("Exception: " + e);
}
}
}
Character Streams:
Character streams are used to read and write text data. Java provides several classes for working with character streams, including
- Reader: Reader is an abstract class that is the superclass of all classes that represent input streams of characters.
- Writer: Writer is also an abstract class that is the superclass of all classes that represent output streams of characters.
- FileReader /FileWriter: FileReader and FileWriter are used to read and write data from and to text files, respectively.
- BufferedReader /BufferedWriter:BufferedReader and BufferedWriter are used to improve the performance of reading and writing data from and to text files.
import java.io.*;
public class CharacterStreamsExample {
public static void main(String[] args) {
try {
FileReader in = new FileReader("input.txt");
FileWriter out = new FileWriter("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
out.close();
} catch(IOException e) {
System.out.println("Exception: " + e);
}
}
}
Usage of Java I/O Streams:
Java I/O streams are used in a variety of scenarios, including reading and writing data from and to files, network communication, and console input and output. Here are some common usage scenarios for Java I/O streams:
Reading and writing data from and to files: Java I/O streams can be used to read and write data from and to files. This is a common scenario for storing and retrieving data in applications.
Network communication: Java I/O streams can be used to send and receive data over a network. This is a common scenario for client-server applications.
Console input and output: Java I/O streams can be used to read and write data to and from the console. This is a common scenario for command-line applications.
Conclusion
Java I/O streams are an essential part of developing applications that handle input and output. By understanding the different types of Java I/O streams and their usage scenarios, developers can write more efficient and effective code.