免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1569 | 回复: 0
打印 上一主题 下一主题

java序列化和反序列化 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-27 09:34 |只看该作者 |倒序浏览
package cn.yicha.ask.AskBaidu;
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.HashMap;import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.log4j.Logger;
public class AskBaidu_Inti {        private static final Logger log = Logger.getLogger(AskBaidu_Inti.class.getName());        public static HashMap askmap = new HashMap();        public static File cachefile =null;                public static void askinit( )        {                cachefile = getCacheFile();                log.info(cachefile.getAbsoluteFile()+"存放序列化文件的路径");                if(!initQueue()){                        askmap = new HashMap();                        log.info("Build mobileBindCheckCodesQueue");                }        }        //构建存放文件的路径        private static File getCacheFile() {                String classpath = System.getProperty("java.io.tmpdir");                String separator = System.getProperty("file.separator");                return new File(classpath+separator+"QueueFile.dat");        }                //反序列化方法        public static boolean initQueue(){                ObjectInputStream in;                try {                        in = new ObjectInputStream(new FileInputStream(cachefile));                        askmap = (HashMap) in.readObject();                        if(askmap != null){                                log.info("Init mobileBindCheckCodesQueue from cache File :"+cachefile);                                return true;                        }                } catch (FileNotFoundException e) {                        e.printStackTrace();                } catch (IOException e) {                        e.printStackTrace();                } catch (ClassNotFoundException e) {                        e.printStackTrace();                }                return false;        }                                //序列化方法        public static void saveQueue(){                ObjectOutputStream out;                try {                        out = new ObjectOutputStream(new FileOutputStream(cachefile));                        out.writeObject(askmap);                        out.flush();                        out.close();                } catch (FileNotFoundException e) {                        e.printStackTrace();                } catch (IOException e) {                        e.printStackTrace();                }                log.info("Save mobileBindCheckCodesQueue to cachefile:"+cachefile);        }    public static void main(String[] args)    {            AskBaidu_Inti ai = new AskBaidu_Inti();            ai.askinit();    }}
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/104127/showart_2079304.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP