Master Java I/O Streams: Byte, Character, Buffered, and More
This article explains Java I/O streams as the mechanism for reading and writing data, covering byte and character streams, their principles, usage steps, buffered and conversion streams, serialization, print streams, and compression streams with clear code examples.
I/O Streams Overview
IO streams are the solution for storing and reading data, analogous to water flowing.
Two main categories: byte streams (operate on all file types) and character streams (operate on text files only).
Byte Streams
FileInputStream reads bytes from a local file.
Typical steps:
Create a FileInputStream object.
Read data using read() or read(byte[]).
Close the stream.
FileInputStream fis = new FileInputStream("example.txt");
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
}
fis.close();FileOutputStream writes bytes to a file.
Create a FileOutputStream object.
Write data using write(int) or write(byte[]).
Close the stream.
FileOutputStream fos = new FileOutputStream("out.txt");
fos.write(97); // writes 'a'
fos.close();Character Streams
FileReader and FileWriter operate on characters, built on top of byte streams with charset conversion.
FileReader fr = new FileReader("text.txt");
int ch;
while ((ch = fr.read()) != -1) {
System.out.print((char) ch);
}
fr.close();FileWriter writes characters to a file.
FileWriter fw = new FileWriter("out.txt");
fw.write('H');
fw.write("Hello");
fw.close();Buffered Streams
Buffered streams add an internal buffer to improve I/O efficiency.
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("data.bin"), 8192);
int b = bis.read();
bis.close(); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("gbk.txt"), "GBK"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();Conversion Streams
Conversion streams bridge byte streams and character streams.
InputStreamReader isr = new InputStreamReader(new FileInputStream("gbk.txt"), Charset.forName("GBK"));
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();Serialization Streams
ObjectOutputStream writes objects to a file; the class must implement Serializable.
Student stu = new Student("zhangsan", 23);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student_object.txt"));
oos.writeObject(stu);
oos.close();ObjectInputStream reads objects back.
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student_object.txt"));
Student stu = (Student) ois.readObject();
System.out.println(stu);
ois.close();Print Streams
PrintStream and PrintWriter provide convenient methods for formatted output.
PrintStream ps = new PrintStream("output.txt");
ps.printf("Name: %s%n", "Alice");
ps.println("---");
ps.close();System.out is a default PrintStream.
PrintStream ps = System.out;
ps.println("123");
// ps.close(); // should not close System.out
ps.println("456");Compression Streams
ZipInputStream and ZipOutputStream handle zip compression and decompression.
public static void unzip(File src, File dest) throws IOException {
ZipInputStream zip = new ZipInputStream(new FileInputStream(src));
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
if (entry.isDirectory()) {
new File(dest, entry.getName()).mkdirs();
} else {
FileOutputStream fos = new FileOutputStream(new File(dest, entry.getName()));
int b;
while ((b = zip.read()) != -1) {
fos.write(b);
}
fos.close();
zip.closeEntry();
}
}
zip.close();
} public static void myZip(File src, File dest) throws IOException {
ZipOutputStream zipos = new ZipOutputStream(new FileOutputStream(new File(dest, "a.zip")));
ZipEntry entry = new ZipEntry("a.txt");
zipos.putNextEntry(entry);
FileInputStream fis = new FileInputStream(src);
int b;
while ((b = fis.read()) != -1) {
zipos.write(b);
}
fis.close();
zipos.closeEntry();
zipos.close();
}Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
