Java对象序列化ObjectOutputStream和ObjectInputStream示例
来源:互联网 发布:上饶网络教育报名时间 编辑:程序博客网 时间:2024/04/30 07:19
Java中ObjectInputStream 与 ObjectOutputStream这两个包装类可用于输入流中读取对象类数据和将对象类型的数据写入到底层输入流 。ObjectInputStream 与 ObjectOutputStream 类所读写的对象必须实现了 Serializable 接口。需要注意的是:对象中的 transient 和 static 类型的成员变量不会被读取和写入 。
具体代码示例:
O bjectFileConvert.java
- package michael.io;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- public class ObjectFileConvert {
- /**
- * 文件转化为Object
- * @param fileName
- * @return byte[]
- */
- public static Object file2Object(String fileName) {
- FileInputStream fis = null;
- ObjectInputStream ois = null;
- try {
- fis = new FileInputStream(fileName);
- ois = new ObjectInputStream(fis);
- Object object = ois.readObject();
- return object;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- if (ois != null) {
- try {
- ois.close();
- } catch (IOException e2) {
- e2.printStackTrace();
- }
- }
- }
- return null;
- }
- /**
- * 把Object输出到文件
- * @param obj
- * @param outputFile
- */
- public static void object2File(Object obj, String outputFile) {
- ObjectOutputStream oos = null;
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(new File(outputFile));
- oos = new ObjectOutputStream(fos);
- oos.writeObject(obj);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (oos != null) {
- try {
- oos.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- if (fos != null) {
- try {
- fos.close();
- } catch (IOException e2) {
- e2.printStackTrace();
- }
- }
- }
- }
- /**
- * @param args
- */
- @SuppressWarnings("unchecked")
- public static void main(String[] args) {
- String fileName = "d:/test/object.obj";
- List<String> list = new ArrayList<String>();
- list.add("michael");
- list.add("大大");
- ObjectFileConvert.object2File(list, fileName);
- System.out.println("success write List<String> to file.");
- List<String> tmpList = (List<String>) ObjectFileConvert
- .file2Object(fileName);
- for (String tmp : tmpList) {
- System.out.println(tmp);
- }
- System.out.println("--------------------------------");
- fileName = "d:/test/uservo.obj";
- UserVo vo = new UserVo("michael", "大大", 18, new Date());
- ObjectFileConvert.object2File(vo, fileName);
- System.out.println("success write bean:UserVo to file.");
- UserVo tmpvo = (UserVo) ObjectFileConvert.file2Object(fileName);
- System.out.println("read bean:UserVo from file get info : " + tmpvo);
- }
- }
UserVo.java
- package michael.io;
- import java.io.Serializable;
- import java.util.Date;
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- public class UserVo implements Serializable {
- /**
- * serialVersionUID
- */
- private static final long serialVersionUID = -6846034858002233878L;
- private String userId;
- private String userName;
- private int age;
- private Date born;
- public UserVo() {
- }
- public UserVo(String userId, String userName, int age, Date born) {
- this.userId = userId;
- this.userName = userName;
- this.age = age;
- this.born = born;
- }
- /**
- * @return the userId
- */
- public String getUserId() {
- return userId;
- }
- /**
- * @return the userName
- */
- public String getUserName() {
- return userName;
- }
- /**
- * @return the age
- */
- public int getAge() {
- return age;
- }
- /**
- * @return the born
- */
- public Date getBorn() {
- return born;
- }
- /**
- * @param pUserId the userId to set
- */
- public void setUserId(String pUserId) {
- userId = pUserId;
- }
- /**
- * @param pUserName the userName to set
- */
- public void setUserName(String pUserName) {
- userName = pUserName;
- }
- /**
- * @param pAge the age to set
- */
- public void setAge(int pAge) {
- age = pAge;
- }
- /**
- * @param pBorn the born to set
- */
- public void setBorn(Date pBorn) {
- born = pBorn;
- }
- @Override
- public String toString() {
- return "userId=[ " + userId + " ] userName=[ " + userName + " ] age=[ "
- + age + " ] born=[ " + born + "] .";
- }
- }
运行结果如下:
success write List<String> to file.
michael
大大
--------------------------------
success write bean:UserVo to file.
read bean:UserVo from file get info : userId=[ michael ] userName=[ 大大 ] age=[ 18 ] born=[ Mon Aug 01 13:49:33 CST 2011] .
0 0
- Java对象序列化ObjectOutputStream和ObjectInputStream示例
- Java对象序列化ObjectOutputStream和ObjectInputStream示例
- 对象序列化ObjectOutputStream,ObjectInputStream
- 对象序列化 ObjectInputStream ObjectOutputStream
- Java对象的序列化ObjectInputStream与ObjectOutputStream
- Java IO--对象序列化Serializable、ObjectOutputStream、ObjectInputStream、transient
- ObjectInputStream 和 ObjectOutputStream(对象的序列化、持久化)
- Java的IO操作(三) - 对象的序列化、ObjectInputStream和ObjectOutputStream类
- java语言编程IO流之对象序列化和ObjectInputStream与ObjectOutputStream
- Java IO之对象的序列化、ObjectInputStream和ObjectOutputStream类
- Java 对象流ObjectInputStream和ObjectOutputStream类
- Java 对象流ObjectInputStream和ObjectOutputStream
- Java ObjectInputStream 和 ObjectOutputStream
- 对象流(ObjectOutputStream 和 ObjectInputStream)
- [Java 12 IO] Serializable 初步 ObjectOutputStream ObjectInputStream 将序列化的对象打出来
- IO流反序列化(objectInputStream)和序列化(ObjectOutputStream)连续存储对象StreamCorruptedException
- Socket同时使用ObjectInputStream和ObjectOutputStream传输序列化对象时的顺序
- Socket同时使用ObjectInputStream和ObjectOutputStream传输序列化对象时的顺序
- cocos2dx-3.x android ant配置实现单工程管理
- Apache HttpServer启动出错处理
- PHP服务器时间
- TextVersion_06_Android性能优化
- django中templates在settings的配置
- Java对象序列化ObjectOutputStream和ObjectInputStream示例
- Leetcode Everyday: 243. Shortest Word Distance
- 个人开公司的流程及费用
- shared_ptr的线程安全性
- 实验报告-5
- 有关Lattice Diamond的若干bug
- TextVersion_07_Fragment相关细节
- Java中的WeakHashMap实现分析
- ExpandListView 的一种巧妙写法