码迷,mamicode.com
首页 > 编程语言 > 详细

Java ObjectOutputStream的使用

时间:2016-06-12 23:21:04      阅读:132      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!