Java IO 基础知识总结

在 Java 编程中,IO(Input/Output,输入 / 输出)操作是至关重要的一部分,它使得程序能够与外部系统进行交互,比如读取文件、写入数据到数据库或者在网络上传输信息等。今天,我们就来深入了解一下 Java IO 的基础知识。

一、流(Stream)的概念

流是 Java IO 的核心抽象,它代表着数据的流动。可以把流想象成一个管道,数据从一端流入,从另一端流出。Java 中有两种基本的流:输入流(InputStream)和输出流(OutputStream),分别用于读取和写入数据。输入流从数据源(如文件、网络连接等)读取数据到程序中,而输出流则将程序中的数据写入到目的地(同样可以是文件、网络等)。

二、字节流与字符流

  1. 字节流
    • 字节流以字节(8 位)为单位处理数据,是最基础的流类型。InputStream 是所有字节输入流的抽象父类,常见的实现类有 FileInputStream,用于从文件中读取字节数据。例如:
复制代码

try (FileInputStream fis = new FileInputStream("test.txt")) {

int data;

while ((data = fis.read())!= -1) {

System.out.print((char) data);

}

} catch (IOException e) {

e.printStackTrace();

}

  • OutputStream 是字节输出流的抽象父类,FileOutputStream 用于向文件写入字节数据:
复制代码

try (FileOutputStream fos = new FileOutputStream("output.txt")) {

String text = "Hello, World!";

byte[] bytes = text.getBytes();

fos.write(bytes);

} catch (IOException e) {

e.printStackTrace();

}

  1. 字符流
    • 字符流以字符(16 位,对于 Unicode 字符)为单位处理文本数据,更适合处理文本文件,因为它能正确处理字符编码。Reader 是字符输入流的抽象父类,FileReader 可读取字符文件:
复制代码

try (FileReader fr = new FileReader("test.txt")) {

int c;

while ((c = fr.read())!= -1) {

System.out.print((char) c);

}

} catch (IOException e) {

e.printStackTrace();

}

  • Writer 是字符输出流的抽象父类,FileWriter 用于写入字符数据:
复制代码

try (FileWriter fw = new FileWriter("output.txt")) {

fw.write("This is a text.");

} catch (IOException e) {

e.printStackTrace();

}

三、缓冲流(Buffered Stream)

缓冲流是为了提高 IO 性能而设计的。它在内部维护了一个缓冲区,减少了直接对底层数据源或目的地的频繁读写操作。BufferedInputStream 和 BufferedOutputStream 是字节缓冲流,BufferedReader 和 BufferedWriter 是字符缓冲流。例如,使用 BufferedReader 读取文件:

复制代码

try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {

String line;

while ((line = br.readLine())!= -1) {

System.out.println(line);

}

} catch (IOException e) {

e.printStackTrace();

}

四、序列化与反序列化

序列化是将对象转换为字节序列以便存储或传输的过程,反序列化则是相反的操作,将字节序列还原为对象。在 Java 中,要使一个对象可序列化,需要实现 Serializable 接口。例如:

复制代码

import java.io.Serializable;

class Person implements Serializable {

private String name;

private int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

}

// getters and setters

}

public class SerializationExample {

public static void main(String[] args) {

Person person = new Person("John", 30);

try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.ser"))) {

oos.writeObject(person);

} catch (IOException e) {

e.printStackTrace();

}

try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.ser"))) {

Person deserializedPerson = (Person) ois.readObject();

System.out.println(deserializedPerson.getName() + ", " + deserializedPerson.getAge());

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

}

}

}

Java IO 体系庞大且复杂,但掌握这些基础知识是深入学习的关键。通过合理运用不同类型的流,我们可以高效地处理各种 IO 任务,让程序与外部世界顺畅沟通。

相关推荐
我不是程序猿儿1 小时前
【C#】 lock 关键字
java·开发语言·c#
tmacfrank2 小时前
网络编程中的直接内存与零拷贝
java·linux·网络
weixin_472339463 小时前
Maven 下载安装与配置教程
java·maven
Magnum Lehar4 小时前
3d游戏引擎EngineTest的系统实现3
java·开发语言·游戏引擎
就叫飞六吧4 小时前
Spring Security 集成指南:避免 CORS 跨域问题
java·后端·spring
Mcworld8574 小时前
java集合
java·开发语言·windows
James. 常德 student4 小时前
网络安全知识点
安全·web安全·php
天黑请闭眼4 小时前
IDEA:程序编译报错:java: Compilation failed: internal java compiler error
java·intellij-idea
南方以南_4 小时前
Ubuntu操作合集
linux·运维·ubuntu
苍煜5 小时前
Maven构建流程详解:如何正确管理微服务间的依赖关系-当依赖的模块更新后,我应该如何重新构建主项目
java·微服务·maven