序列化对象输入输出操作实例
2010-10-28 15:56
519 查看
/** * 将序列化对象写入文件 * @param obj 对象 * @param path 路径 * @return 是否写入成功 */ public static boolean writeObject2File(Object obj,String path) { FileOutputStream ostream = null; ObjectOutputStream p = null; try { ostream = new FileOutputStream(path); p = new ObjectOutputStream(ostream); p.writeObject(obj); p.flush(); } catch (IOException e) { PubUtil.logDebug("序列化对象写入文件错误"+e); return false; } finally{ try { if(ostream!=null) ostream.close(); } catch (IOException e) { PubUtil.logDebug("关闭ostream流错误"+e); e.printStackTrace(); } } return true; } /** * 读取序列化对象文件 * @param path 路径及名称 * @return 对象 */ public static Object readObjectFromFile(String path){ FileInputStream fis = null; ObjectInputStream ois = null; Object obj = null; // PubUtil.logDebug("开始读取序列化文件"+path); try { fis = new FileInputStream(path); ois = new ObjectInputStream(fis); obj = ois.readObject(); } catch (ClassNotFoundException e) { PubUtil.logDebug("读取序列化对象,发生转换错误"+e); } catch (FileNotFoundException e) { PubUtil.logDebug("读取序列化对象错误,无该对象"+e); } catch (IOException e) { PubUtil.logDebug("读取序列化对象错误"+e); } finally{ try { if(ois!=null){ ois.close(); } } catch (IOException e) { PubUtil.logDebug("关闭ois流错误"+e); } } // PubUtil.logDebug("读取结束"); return obj; } /** * 将序列化对象输出为XML文件 * @param obj 序列化对象 * @param fileName 路径及文件名 */ public static boolean objectXmlEncoder(Object obj,String fileName){ File fo = null; FileOutputStream fos = null; XMLEncoder encoder = null; try { fo = new File(fileName); if(!fo.exists()){ String path = fileName.substring(0,fileName.lastIndexOf('.')); File pFile = new File(path); pFile.mkdirs(); } fos = new FileOutputStream(fo); encoder = new XMLEncoder(fos); encoder.writeObject(obj); encoder.flush(); } catch (FileNotFoundException e) { PubUtil.logDebug("输出序列化对象XML,发生错误"+e); } finally{ try { if(encoder!=null){ encoder.close(); } if(fos!=null){ fos.close(); } } catch (IOException e) { PubUtil.logDebug("关闭流错误"+e); } } return true; } /** * 读取序列化对象XML文件 * @param objSource * @return */ public static Object objectXmlDecoder(String objSource){ // List objList = null; File fin = null; FileInputStream fis = null; XMLDecoder decoder = null; Object obj = null; try { // objList = new ArrayList(); fin = new File(objSource); fis = new FileInputStream(fin); decoder = new XMLDecoder(fis); obj = decoder.readObject(); } catch (IOException e) { PubUtil.logDebug("读取序列化对象XML,发生错误"+e); } finally{ try{ if(decoder!=null){ decoder.close(); } if(fis!=null){ fis.close(); } }catch(IOException e){ PubUtil.logDebug("关闭流错误"+e); } } return obj; }
相关文章推荐
- java 关于中文字的输出 输入 和文件操作(对象序列化)
- java 对象输入输出流读写文件的操作实例
- 定义一个List集合并实例化对应的实现类对象。循环输入任意个整数,输入0结束输入循环,对集合中的数据进行从大到小的排序后输出
- 不改变使用习惯的情况下为std::stream对象的输入输出操作附加线程安全
- 输入输出之对象序列化Serializable
- Python-输入/输出 操作
- python数字类型介绍以及创建数字值对象引用、删除数字值引用操作实例
- Java基础——对象序列化+管道流+RandomAccessFile+操作基本数据类型的DataStream等
- Python3基础之输入和输出实例分析
- 使用ADO.NET对象调用存储过程的输入和输出参数
- 使用对象输入输出流(ObjectOutputStream,ObjectInputStream)实现序列化对象的深度克隆
- Android实现Parcelable对象序列化的实例
- C#操作Xml:XmlSerializer 对象的Xml序列化和反序列化
- Android For JNI(二)——C语言中的数据类型,输出,输入函数以及操作内存地址,内存修改器
- Python3 输入和输出以及文件操作
- 1.输入输出操作格式对齐问题
- jsp隐式对象之输入和输出对象
- 用popen函数操作其它程序的输入和输出
- JavaCookbook-9.面向对象的技术10.输入和输出
- 输入输出流操作对象