首页 > 编程语言 > 详细

Java ObjectOutputStream的使用

时间:2016-06-12 23:17:53      阅读:236      评论:0      收藏:0      [点我收藏+]
package com.qiqiao.test2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 对象<=>文件转换类 
 * @author Administrator
 *
 */
public class ObjectFileConvert {


    public static void object2File(Object obj,File outFile){
        ObjectOutputStream oos = null;
        FileOutputStream fos = null;
        try {
            if(!outFile.exists()){
                outFile.createNewFile();
            }
            fos = new FileOutputStream(outFile);
            oos = new ObjectOutputStream(fos);
            
            oos.writeObject(obj);
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            if (oos != null) {
                try {
                    oos.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
    }
    
    
    public static Object file2Object(File inFile){
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(inFile);
            ois = new ObjectInputStream(fis);
            
            
            return ois.readObject();
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            if(ois!= null){
                try {
                    ois.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
            }
        }
        return null;
    }
}

 

Java ObjectOutputStream的使用

原文:http://www.cnblogs.com/perfectzk/p/5579045.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!