Name the main classes for working with input and output streams in Java.
sobes.tech AI
Answer from AI
The main classes for working with input/output streams in Java are located in the java.io package. They are divided into byte streams and character streams.
Byte streams:
Work with bytes (8 bits):
- For input (Input Streams):
InputStream: Abstract base class for all byte input streams.FileInputStream: For reading bytes from a file.ByteArrayInputStream: For reading bytes from a byte array in memory.BufferedInputStream: Buffers input for performance enhancement.DataInputStream: Reads primitive data types (int, float, etc.) in binary format.ObjectInputStream: Deserializes objects from a stream.
- For output (Output Streams):
OutputStream: Abstract base class for all byte output streams.FileOutputStream: For writing bytes to a file.ByteArrayOutputStream: For writing bytes to a byte array in memory.BufferedOutputStream: Buffers output for performance enhancement.DataOutputStream: Writes primitive data types in binary format.ObjectOutputStream: Serializes objects into a stream.
Character streams:
Work with characters (usually 16 bits in Unicode):
- For input (Readers):
Reader: Abstract base class for all character input streams.FileReader: For reading characters from a file (uses default encoding).StringReader: For reading characters from a string.BufferedReader: Buffers character input for performance enhancement.InputStreamReader: Converts byte streams to character streams, specifying encoding.
- For output (Writers):
Writer: Abstract base class for all character output streams.FileWriter: For writing characters to a file (uses default encoding).StringWriter: For writing characters to a string.BufferedWriter: Buffers character output for performance enhancement.OutputStreamWriter: Converts character streams to byte streams, specifying encoding.PrintWriter: An improved version of Writer with convenient methods for printing various data types.
Usage example:
Reading a file line by line:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
// Example of reading a file line by line
public class ReadFileExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("my_file.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Writing to a file:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
// Example of writing to a file
public class WriteFileExample {
public static void main(String[] args) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
bw.write("This is the first line.");
bw.newLine(); // Write a new line
bw.write("This is the second line.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
For more complex operations (working with objects, serialization/deserialization), ObjectInputStream and ObjectOutputStream are used.
The choice between byte streams and character streams depends on the data type: binary data (images, sounds) use byte streams, while text data use character streams. Decorator classes (e.g., BufferedInputStream, DataInputStream) add additional functionality or improve performance of basic streams. Using try-with-resources guarantees automatic closing of streams.