Java对象序列化ObjectOutputStream和ObjectInputStream示例
程序员文章站
2022-04-03 19:53:28
...
blog迁移至 :http://www.micmiu.com
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] .
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] .
本文连接:http://sjsky.iteye.com/blog/1137231
转载请注明来自:Michael's blog @ http://sjsky.iteye.com
----------------------------- 分 ------------------------------ 隔 ------------------------------ 线 ------------------------------
上一篇: win10中文用户名改英文
下一篇: 高级讲解MySQL查询缓存优化
推荐阅读
-
Redis缓存Object,List对象 - Java 对象和List的序列化和反序列化
-
【Java】Java深入理解io篇—数据流,对象流和序列化
-
Java深度学习系列——对象流和序列化
-
java 基础之 I/O流 其四 (对象流和序列化)
-
对象流,它们是一对高级流,负责即将java对象与字节之间在读写的过程中进行转换。 * java.io.ObjectOutputStream * java.io.ObjectInputStream
-
Java基础学习总结——Java对象的序列化和反序列化
-
Java序列化和反序列化示例介绍
-
序列化和反序列化(六)——Java对象的网络传输(一)
-
Jackson序列化和反序列化java对象
-
Java中对象的序列化和反序列化操作