自定义对象
package com.example.demo;
import java.io.Serializable;
class User implements Serializable {
private static final long serialVersionUID = 686362938982330454L;
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = 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
demo
class DemoApplicationTests {
public static final String filepath = "C:\\Users\\seczone\\Desktop\\test3.class";
@Test
void contextLoads() throws Exception {
Map<Object, Object> map = new HashMap<>();
map.put("字符", "1");
map.put("数字", 1);
map.put("char", '1');
map.put("boolean", true);
List<Object> list = new ArrayList<>();
list.add(map);
list.add("list");
User user = new User();
user.setName("xsp");
user.setAge(18);
FileOutputStream fos = new FileOutputStream(filepath);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fos);
objectOutputStream.writeObject(map);
objectOutputStream.writeObject(list);
objectOutputStream.writeObject(user);
objectOutputStream.close();
fos.close();
}
@Test
void test() throws Exception {
FileInputStream fis = new FileInputStream(filepath);
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println(ois.readObject());
System.out.println(ois.readObject());
System.out.println(ois.readObject());
ois.close();
fis.close();
}
}
- 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
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47