Skip to content

Commit 30330cb

Browse files
committed
feat:IO流、序列化和反序列化
1 parent a3a6b21 commit 30330cb

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

src/posts/杂谈/JavaIO流.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
```
2+
title: IO流
3+
date: 2025/10/2
4+
categories:
5+
- 杂谈
6+
tags:
7+
- IO流
8+
```
9+
# JavaIO流
10+
## IO
11+
* O是Input和Output,以内存为界限,Input是从外界读取诗句到内存,Output是从内存中取出数据到外界。
12+
* IO流是一种顺序读写数据的模式,它的特点是单向流动。
13+
### 字节流
14+
Java中的InputStream和OutputStream是以字节为基本单位的,InputStream代表输入字节流,OutputStream代表输出字节流
15+
### 字符流
16+
Java中的Reader和Writer是以字符为基本单位的,Reader代表字符输入流,Writer代表字符输出流
17+
### 同步和异步
18+
同步IO是指,读写IO时代码必须等待IO操作结束后才继续执行后续代码,它的优点是代码编写简单,缺点是CPU执行效率低。
19+
20+
而异步IO是指,读写IO时仅发出请求,然后立刻执行后续代码,它的优点是CPU执行效率高,缺点是代码编写复杂。
21+
22+
## File对象
23+
File 对象是文件或目录路径的抽象,不代表文件内容,也不能读写内容。
24+
构建File对象需要传入路径,可以是相对路径,也可以是绝对路径
25+
26+
File用来:
27+
28+
* 创建 / 删除
29+
30+
创建
31+
```java
32+
file.createNewFile();
33+
file.mkdir(); // 只能创建一级目录
34+
file.mkdirs(); // 创建多级目录(常用)
35+
```
36+
37+
删除
38+
39+
file.delete(); //目录必须是空目录
40+
* 判断存在性
41+
```java
42+
f.exists(); // 是否存在
43+
f.isFile(); // 是否是文件
44+
f.isDirectory(); // 是否是目录
45+
```
46+
* 获取属性
47+
```java
48+
f.getName(); // 文件名
49+
f.length(); // 文件大小(字节)
50+
f.lastModified(); // 最后修改时间(时间戳)
51+
```
52+
* 遍历目录
53+
```java
54+
String[] names = dir.list();
55+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
```
2+
title: 序列化和反序列化
3+
date: 2025/12/15
4+
categories:
5+
- 杂谈
6+
tags:
7+
- 序列化
8+
```
9+
# 序列化和反序列化
10+
## 序列化
11+
将对象转换为可存储或可传输的字节流或其他数据格式的**过程**
12+
依赖FastJSON的序列化(序列化为JSON)
13+
```java
14+
public static void main(String[] args) throws Exception {
15+
User user = new User();
16+
user.setAge(18);
17+
user.setUserName("张三");
18+
String jsonString = JSONObject.toJSONString(user);
19+
File file = new File("user.json");
20+
Files.write(file.toPath(), jsonString.getBytes(StandardCharsets.UTF_8));
21+
System.out.println(file.length());
22+
}
23+
```
24+
将对象序列化为字节数组
25+
```java
26+
private static byte[] serialize(User user) {
27+
String name = user.getUserName();
28+
byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
29+
ByteBuffer buffer = ByteBuffer.allocate(nameBytes.length + Integer.BYTES);
30+
buffer.putInt(user.getAge());
31+
buffer.put(nameBytes);
32+
return buffer.array();
33+
}
34+
35+
```
36+
对象实现serializable的序列化
37+
```java
38+
private static byte[] serialize(User user) throws IOException {
39+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
40+
ObjectOutputStream oob = new ObjectOutputStream(baos);
41+
oob.writeObject(user);
42+
return baos.toByteArray();
43+
}
44+
```
45+
## 反序列化
46+
将字节流或其他格式的数据还原为对象的过程
47+
48+
依赖FastJSON的反序列化
49+
```java
50+
public static void main(String[] args) throws Exception {
51+
File file = new File("user.json");
52+
byte[] bytes = Files.readAllBytes(file.toPath());
53+
User user = JSONObject.parseObject(new String(bytes, StandardCharsets.UTF_8), User.class);
54+
System.out.println(user);
55+
}
56+
```
57+
将字节数组反序列化为对象
58+
```java
59+
private static User deserialize(byte[] bytes){
60+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
61+
int age = buffer.getInt();
62+
byte[] nameBytes = new byte[buffer.remaining()];
63+
buffer.get(nameBytes);
64+
User user = new User();
65+
user.setAge(age);
66+
user.setUserName(new String(nameBytes, StandardCharsets.UTF_8));
67+
return user;
68+
}
69+
```
70+
对象实现serializable的反序列化
71+
```java
72+
private static User deserialize(byte[] bytes) throws Exception {
73+
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
74+
ObjectInputStream objectInputStream = new ObjectInputStream(bais);
75+
return (User) objectInputStream.readObject();
76+
}
77+
```

0 commit comments

Comments
 (0)