Java IO流
IO流简介
Java IO(Input/Output)流是Java中用于处理输入输出的机制。它提供了丰富的类来处理不同类型的数据流。
IO流的分类
// IO流的主要分类
InputStream/OutputStream(字节流)
├── FileInputStream/FileOutputStream
├── BufferedInputStream/BufferedOutputStream
├── DataInputStream/DataOutputStream
└── ObjectInputStream/ObjectOutputStream
Reader/Writer(字符流)
├── FileReader/FileWriter
├── BufferedReader/BufferedWriter
├── InputStreamReader/OutputStreamWriter
└── PrintWriter
字节流
字节流用于处理二进制数据,以字节为单位进行读写操作。
// 文件复制示例
try (FileInputStream fis = new FileInputStream("source.txt");
FileOutputStream fos = new FileOutputStream("target.txt")) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
System.out.println("文件复制完成");
} catch (IOException e) {
e.printStackTrace();
}
// 使用缓冲流提高性能
try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream("large.txt"));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("copy.txt"))) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
System.out.println("大文件复制完成");
} catch (IOException e) {
e.printStackTrace();
}
字符流
字符流用于处理文本数据,以字符为单位进行读写操作。
// 文本文件读写示例
try (FileReader reader = new FileReader("input.txt");
FileWriter writer = new FileWriter("output.txt")) {
char[] buffer = new char[1024];
int charsRead;
while ((charsRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, charsRead);
}
System.out.println("文本文件复制完成");
} catch (IOException e) {
e.printStackTrace();
}
// 使用缓冲字符流
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
System.out.println("文本文件按行复制完成");
} catch (IOException e) {
e.printStackTrace();
}
数据流
数据流用于读写基本数据类型和字符串。
// 写入数据
try (DataOutputStream dos = new DataOutputStream(
new FileOutputStream("data.txt"))) {
dos.writeInt(100);
dos.writeDouble(3.14);
dos.writeUTF("Hello, World!");
dos.writeBoolean(true);
System.out.println("数据写入完成");
} catch (IOException e) {
e.printStackTrace();
}
// 读取数据
try (DataInputStream dis = new DataInputStream(
new FileInputStream("data.txt"))) {
int intValue = dis.readInt();
double doubleValue = dis.readDouble();
String stringValue = dis.readUTF();
boolean booleanValue = dis.readBoolean();
System.out.println("读取的数据:");
System.out.println("整数:" + intValue);
System.out.println("浮点数:" + doubleValue);
System.out.println("字符串:" + stringValue);
System.out.println("布尔值:" + booleanValue);
} catch (IOException e) {
e.printStackTrace();
}
对象流
对象流用于序列化和反序列化对象。
// 可序列化的类
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// getter和setter方法
}
// 序列化对象
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("person.ser"))) {
Person person = new Person("张三", 25);
oos.writeObject(person);
System.out.println("对象序列化完成");
} catch (IOException e) {
e.printStackTrace();
}
// 反序列化对象
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("person.ser"))) {
Person person = (Person) ois.readObject();
System.out.println("反序列化结果:");
System.out.println("姓名:" + person.getName());
System.out.println("年龄:" + person.getAge());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
NIO(New IO)
Java NIO提供了更高效的IO操作方式。
// 使用NIO复制文件
try {
Path source = Paths.get("source.txt");
Path target = Paths.get("target.txt");
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
System.out.println("文件复制完成");
} catch (IOException e) {
e.printStackTrace();
}
// 使用NIO读取文件内容
try {
Path path = Paths.get("input.txt");
List lines = Files.readAllLines(path, StandardCharsets.UTF_8);
for (String line : lines) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// 使用NIO写入文件
try {
Path path = Paths.get("output.txt");
List lines = Arrays.asList("第一行", "第二行", "第三行");
Files.write(path, lines, StandardCharsets.UTF_8);
System.out.println("文件写入完成");
} catch (IOException e) {
e.printStackTrace();
}
实践练习
练习1:文件加密解密
public class FileEncryptor {
// 实现文件加密方法
public static void encrypt(String inputFile, String outputFile) {
// 实现加密逻辑
}
// 实现文件解密方法
public static void decrypt(String inputFile, String outputFile) {
// 实现解密逻辑
}
}