对对象包装类ObjectInputStream/ObjectOutputStream的应用
来源:互联网 发布:len在vb中什么意思 编辑:程序博客网 时间:2024/05/23 18:55
实现代码如下:
Student.java文件的代码:
import java.io.*;public class Student implements Serializable{ int id; String name; int age; String department; public Student(int id,String name,int age,String department) { this.id = id; this.name = name; this.age = age; this.department = department; }}
Serialization.java文件的代码:
import java.io.*;public class Serialization{ public static void main(String[] args) throws Exception { Student stu1 = new Student(19,"zhangsan",25,"huaxue"); Student stu2 = new Student(20,"lisi",23,"wuli"); FileOutputStream fos = new FileOutputStream("Student.txt"); ObjectOutputStream os = new ObjectOutputStream(fos); os.writeObject(stu1); os.writeObject(stu2); os.close(); FileInputStream fis = new FileInputStream("Student.txt"); ObjectInputStream is = new ObjectInputStream(fis); Student stu3 = (Student)is.readObject(); Student stu4 = (Student)is.readObject(); is.close(); System.out.println("id:"+stu3.id); System.out.println("name:"+stu3.name); System.out.println("age:"+stu3.age); System.out.println("department:"+stu3.department); System.out.println("id:"+stu4.id); System.out.println("name:"+stu4.name); System.out.println("age:"+stu4.age); System.out.println("department:"+stu4.department); }}
运行结果:
Student.txt文件里面是乱码,因为使用对象包装类写入的内容只有此对象包装类才才能对写出正确的内容。使用对象包装类的好处就是可以直接实现对对象的读写,减少了很多操作。
0 0
- 对对象包装类ObjectInputStream/ObjectOutputStream的应用
- ObjectOutputStream-ObjectInputStream的使用
- ObjectInputStream与ObjectOutputStream类
- ObjectInputStream和ObjectOutputStream类
- ObjectOutputStream 和 ObjectInputStream的使用
- 对对象的理解
- ObjectOutputStream 和 ObjectInputStream类的简单介绍,及运用。
- ObjectInputStream与ObjectOutputStream类实现对象的存取
- ObjectInputStream ObjectOutputStream
- ObjectInputStream ObjectOutputStream
- ObjectOutputStream/ObjectInputStream
- ObjectInputStream & ObjectOutputStream
- ObjectOutputStream/ObjectInputStream
- ObjectInputStream与ObjectOutputStream的顺序问题
- java中的ObjectInputStream和ObjectOutputStream的讲解
- ObjectInputStream与ObjectOutputStream的顺序问题
- java对象流ObjectInputStream、ObjectOutputStream的使用
- java中ObjectOutputStream和ObjectInputStream的使用
- 解决SVN客户端修改log message失败的问题
- Spring理论知识点
- baiyun
- eclipse与cmake配合使用
- 分布式服务框架DUBBO(一)DUBBO简介
- 对对象包装类ObjectInputStream/ObjectOutputStream的应用
- [BZOJ3048][Usaco2013 Jan]Cow Lineup(单调队列)
- SPOJ QTREE4 Query on a tree IV(边分治)
- Day 2 of traveling
- 如何解决maven搭建项目的时候,src/main/java无法建立的问题,提示信息The folder is already a source folder.
- GitHub&&SourceTree教程
- SPOJ QTREE5Query on a tree V(边分治)
- Socket编程—TcpListener和TcpClient的使用
- 用UDP编写网络聊天程序