关于存储、读入Object,贴两个方法
2011-07-15 11:11
211 查看
生成.sdua文件的时候可能会用到读写对象,贴两个方法……
另外,
1、需要读写的对象需要实现序列化接口(
xxxx implements Serializable
)
2、返回Object可以用instanceoff关键字判断是否是某一种对象,例如
if(object instanceof int[]){
System.out.println("length of the arr is : "+ ((int[])object).length);
}
/**写对象方法,参数是路径和对象*/
public static void writeObject(String outFile, Object object) { try { ObjectOutputStream out = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream(outFile))); out.writeObject(object); out.close(); } catch (Exception e) { System.err.println(e); } }
/**写对象方法,参数是对象路径,返回Object对象*/
public static Object readObject(String filePath) { File inFile = new File(filePath); if(!inFile.exists()){ return null; } Object o = null; try { ObjectInputStream in = new ObjectInputStream( new BufferedInputStream(new FileInputStream(inFile))); o = in.readObject(); in.close(); } catch (Exception e) { System.out.println(e); } return o; }
另外,
1、需要读写的对象需要实现序列化接口(
xxxx implements Serializable
)
2、返回Object可以用instanceoff关键字判断是否是某一种对象,例如
if(object instanceof int[]){
System.out.println("length of the arr is : "+ ((int[])object).length);
}
相关文章推荐
- 关于本地缓存localstorage与sessionStorage 数组 (array)字符串(string) 对象(object)的存储技巧和注意事项
- 两个修改Docker本地镜像与容器的存储位置的方法
- 关于jquery html()方法获取带有OBJECT标签的元素内容时,出现“类型不匹配。”的解决办法
- "getline" bug fix for Microsoft Visual C++ 6.0 关于VC6的getline输入需要两个回车才结束的BUG修改方法
- 关于ios object-c 类别-分类 category 的静态方法与私有变量,协议 protocol&nbsp
- 关于两个时间(00:00:00)相加的实现方法
- python两个关于计算方法运行时间的修饰器
- 关于Matlab 读入图像方法
- 关于ios object-c 类别-分类 category 的静态方法与私有变量,协议 protocol
- 关于 字符串中的单引号, 在sql存储过程中的传输总结; 逗号分隔的多个变量提取方法.
- 关于android中数据持久化存储的方法的知识整理
- 关于List的toString方法调用的是否是父类Object的,explise显示是的,但其实不是,是AbstractCollection的,他复写了Object的toString
- JSONObject遍历获取键值方法合并两个JSONObject
- 关于Object类的equals方法
- sql server中破解加密的存储过程 视图 等object的方法
- 【转】第4篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:委托回调方法分析
- 关于百度富文本编辑器ueditor的.NET版本地图片上传提示uploader类同时存在于两个dll中的解决方法
- 关于存储过程中一个参数表示多个查询条件使用方法的简单处理
- 关于java方法中Object... args参数的含义
- 关于Eclipse RCP的两个小问题和解决方法