序列化
来源:互联网 发布:郭德纲网络节目 编辑:程序博客网 时间:2024/05/29 10:46
序列化是将对象状态转换为可保持或传输的格式的过程。
与序列化相对的是反序列化,它将流转换为对象。
这两个过程结合起来,可以轻松地存储和传输数据。
public static void main(String[] args)throws IOException {List<Dog> list=new ArrayList<Dog>();Dog dog=new Dog("dd",18);Dog dog1=new Dog("dsd",18); list.add(dog); list.add(dog1);FileOutputStream fos=new FileOutputStream("E:/S2226.txt");ObjectOutputStream oos=new ObjectOutputStream(fos);oos.writeObject(list);fos.close();oos.close();System.out.println("序列化成功!");}
Dog 类
import java.io.Serializable;public class Dog implements Serializable{ private String name; private 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;}public Dog(String name, int age) {super();this.name = name;this.age = age;}public Dog() {super();// TODO Auto-generated constructor stub}
注意点:
01.如果自定义类需要被序列化,那么必须实现Serializable接口
02.禁止某个属性被序列化
使用
transient
修饰
比如:
transient
public
String msg
0 0
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- 序列化
- [SDOI2009] 洛谷P1972 HH的项链-------离线方法 && 在线主席树
- Android代码连接Wifi时被系统切换到其他Wifi的问题
- css 实现垂直居中的 N 种场景及 N 种方法
- java基础day06
- HDU1754
- 序列化
- request和formcollet的用法及细节
- git解决文件冲突
- Recat Native android 环境搭建
- TCP协议的“三握四挥”过程简述
- GDSOI2017模拟
- MySQL数据库(3)
- Doing Homework again
- 九度OJ题目1013:开门人和关门人解题报告