Java输入输出流
# 概述
Java的输入输出流用于读取和写入字节数据。Java的输入输出流分为输入流和输出流,输入流用于读取字节数据,输出流用于写入字节数据。Java的输入输出流类位于java.io
包中。
# 常用输入流
InputStream
:所有输入流的基类,用于读取字节数据。FileInputStream
:从文件中读取字节。BufferedInputStream
:提供缓冲功能的输入流。DataInputStream
:提供对基本数据类型和对象进行序列化和反序列化的输入流。ObjectInputStream
:提供对对象进行序列化和反序列化的输入流。ByteArrayInputStream
:从字节数组中读取字节。- ......
# 常用输出流
OutputStream
:所有输出流的基类,用于写入字节数据。FileOutputStream
:向文件中写入字节。BufferedOutputStream
:提供缓冲功能的输出流。DataOutputStream
:提供对基本数据类型和对象进行序列化和反序列化的输出流。ObjectOutputStream
:提供对对象进行序列化和反序列化的输出流。ByteArrayOutputStream
:向字节数组中写入字节。- ......
# 示例代码
import java.io.*;
public class Test {
public static void main(String[] args) {
try {
// 写入文件
FileOutputStream fos = new FileOutputStream("test.txt");
fos.write("Hello, world!".getBytes());
fos.close();
// 读取文件
FileInputStream fis = new FileInputStream("test.txt");
int data = 0;
while ((data = fis.read())!= -1) {
System.out.print((char) data);
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 常用方法
read()
:从输入流中读取一个字节。read(byte[] b)
:从输入流中读取字节数组。read(byte[] b, int off, int len)
:从输入流中读取字节数组的某一部分。skip(long n)
:跳过输入流中的n
字节。available()
:返回输入流中可读取的字节数。close()
:关闭输入流。flush()
:刷新输出流。write(int b)
:向输出流中写入一个字节。write(byte[] b)
:向输出流中写入字节数组。write(byte[] b, int off, int len)
:向输出流中写入字节数组的某一部分。close()
:关闭输出流。
# 注意事项
- 输入流和输出流都需要通过
close()
方法关闭。 - 输入流和输出流都可以用
try-catch
块来捕获异常。 - 输入流和输出流都可以用
available()
方法获取可读取的字节数。 - 输入流和输出流都可以用
flush()
方法刷新缓冲区。
# 序列化和反序列化
Java的序列化和反序列化是指将对象转换为字节序列,并在需要时恢复对象。Java提供了ObjectOutputStream
和ObjectInputStream
类来实现序列化和反序列化。
ObjectOutputStream
:用于序列化对象。ObjectInputStream
:用于反序列化对象。DataInputStream
:用于反序列化基本数据类型和对象。DataOutputStream
:用于序列化基本数据类型和对象。
# ObjectInputStream和ObjectOutputStream
示例代码:
import java.io.*;
public class Test {
public static void main(String[] args) {
try {
// 序列化
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.txt"));
oos.writeObject(new Person("Tom", 20));
oos.close();
// 反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));
Person person = (Person) ois.readObject();
System.out.println(person.getName() + " " + person.getAge());
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# DataInputStream和DataOutputStream
示例代码:
import java.io.*;
public class Test {
public static void main(String[] args) {
try {
// 序列化
DataOutputStream dos = new DataOutputStream(new FileOutputStream("test.txt"));
dos.writeUTF("Hello, world!");
dos.writeInt(123456);
dos.close();
// 反序列化
DataInputStream dis = new DataInputStream(new FileInputStream("test.txt"));
String str = dis.readUTF();
int num = dis.readInt();
System.out.println(str + " " + num);
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 区别
- 序列化和反序列化是指将对象转换为字节序列,并在需要时恢复对象。
ObjectOutputStream
和ObjectInputStream
类用于对象实现序列化和反序列化。DataInputStream
和DataOutputStream
类用于对基本数据类型和对象进行序列化和反序列化。
# 总结
- Java的输入输出流用于读取和写入字节数据。
- 输入流和输出流分别由
InputStream
和OutputStream
类表示。 - 常用的输入流包括
FileInputStream
、BufferedInputStream
、ObjectInputStream
、ByteArrayInputStream
和DataInputStream
。 - 常用的输出流包括
FileOutputStream
、BufferedOutputStream、ObjectOutputStream
、ByteArrayOutputStream
和DataOutputStream
。
最后更新时间: 2025/01/02, 14:49:49