对象的读写
使用ObjectInputStream和ObjectOutputStream读写对象(序列化与反序列化)。
只有字节流没有字符流
- .类必须实现Serializable接口
- 给类加个序列化编号,给类定义一个标记,新的修改后的类还可以操作曾经序列化的对象
- 静态是不能被序列化的,序列化只能对堆中的进行序列化 ,不能对“方法区”中的进行序列化
- 不需要序列化的字段前加 transient
小例子:
先创建一个Dog对象并序列化:
?
1
2
3
4
5
6
7
8
9
|
package com.uwo9.test03;
import java.io.Serializable;
public class Dog implements Serializable {
private static final long serialVersionUID = 2809685095868158625L;
String name;
String color;
}
|
再创建一个Student对象并序列化:
?
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
|
package com.uwo9.test03;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 9078616504949971001L;
static public String schoolName;
private transient String name;
private transient int age;
private double score;
private Dog dog;
public Student() {
super ();
}
public Student(String name, int age, double score, Dog dog) {
super ();
this .name = name;
this .age = age;
this .score = score;
this .dog = dog;
}
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;
}
public double getScore() {
return score;
}
public void setScore( double score) {
this .score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + "]" ;
}
}
|
将数据写入对象流并存入文件
?
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
|
package com.uwo9.test03;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
public class Test01 {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "大黄" ;
dog.color = "Yellow" ;
Student student1 = new Student( "学生1" , 18 , 99 ,dog);
Student student2 = new Student( "学生2" , 19 , 99 ,dog);
Student student3 = new Student( "学生3" , 20 , 99 ,dog);
Student.schoolName = "某某大学" ;
File file = new File( "E:/Temp/Test1.txt" );
ObjectOutputStream oos = null ;
try {
oos = new ObjectOutputStream( new FileOutputStream(file));
//oos.writeObject(student);
ArrayList<Student> arrayList = new ArrayList<>();
Collections.addAll(arrayList, student1,student2,student3);
oos.writeObject(arrayList);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
oos.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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package com.uwo9.test03;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class Test02 {
public static void main(String[] args) {
// 从指定的文件中读取对象
File file = new File( "E:/Temp/Test1.txt" );
ObjectInputStream ois= null ;
try {
ois = new ObjectInputStream( new FileInputStream(file));
// 读取对象
// Student stu = (Student)ois.readObject();
// System.out.println("读取到的数据为:"+stu);
@SuppressWarnings ( "unchecked" )
ArrayList<Student> arrayList = (ArrayList<Student>) ois.readObject();
for (Student student : arrayList) {
System.out.println(student);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
|
到此这篇关于Java操作IO对象流进行数据的读写的文章就介绍到这了,更多相关Java IO流进行数据的读写内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://blog.csdn.net/huanyinghanlang/article/details/78828347
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 40
-
CSS in JS 新秀:Vanilla-Extract 浅析
2025-05-29 31 -
2025-06-04 85
-
2025-05-29 25
-
2025-05-29 18
热门评论