java的Serializable功能
2016-07-20 15:20
573 查看
可以将java对象序列化为文件,也可以将文件反序列化为java对象
原创不易,转载请注明出处:分享java的Serializable功能
DeserializeFile2Object.java
[java] view plain copy print?
- package com.zuidaima.util;
- import java.io.FileInputStream;
- import java.io.ObjectInputStream;
- /**
- * 反序列化文件到java对象
- *
- * @author javaniu
- *
- */
- public class DeserializeFile2Object {
- public static void main(String[] args) {
- Address address = deserialzeAddress();
- System.out.println(address);
- }
- public static Address deserialzeAddress() {
- Address address;
- try {
- FileInputStream fin = new FileInputStream("c:/address.ser");
- ObjectInputStream ois = new ObjectInputStream(fin);
- address = (Address) ois.readObject();
- ois.close();
- return address;
- } catch (Exception ex) {
- ex.printStackTrace();
- return null;
- }
- }
- }
SerializeObject2File.java
[java] view plain copy print?
- package com.zuidaima.util;
- import java.io.FileOutputStream;
- import java.io.ObjectOutputStream;
- /**
- * 序列化java对象到文件
- *
- 1bb8b * @author javaniu
- *
- */
- public class SerializeObject2File {
- public static void main(String args[]) {
- serializeAddress("北京朝阳区", "中国");
- }
- public static void serializeAddress(String street, String country) {
- Address address = new Address();
- address.setStreet(street);
- address.setCountry(country);
- try {
- FileOutputStream fout = new FileOutputStream("c:\\address.ser");
- ObjectOutputStream oos = new ObjectOutputStream(fout);
- oos.writeObject(address);
- oos.close();
- System.out.println("Done");
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
相关文章推荐
- 分享java的Serializable功能
- 分享java的Serializable功能
- java.IO.Serializable 接口是什么功能?
- 【Java面试题】之分页功能的实现
- java利用POI导出Excel功能实现
- 用java实现打印功能
- Java JSP实现页面打印的功能
- JAVA中用动态代理类实现记忆功能(一)
- Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用
- Java服务端支付功能模块--(二)微信支付
- java.io.NotSerializableException错误解决方法
- Java实现视频网站的视频上传、视频转码、视频关键帧抽图, 及视频播放功能 视频网站中提供的在线视频播放功能,播放的都是FLV格式的文件,它是Flash动画文件,可通过Flash制作的播放器来播
- Java基础知识强化75:正则表达式之分割功能(字符串中的数字排序案例)
- java 下载功能的实现
- java中struts2实现文件上传下载功能
- 关于JavaWeb如何实现用户类似站内信功能
- JavaSwing_5.5: 拖拽功能
- tomcat下java.io.NotSerializableException错误的解决方法
- JAVA--实现类似C#输入功能的Console类
- [置顶] Android开发之SDCardUtils工具类。java工具详细代码,附源代码。判断SD卡是否挂载等功能