首页 > 代码库 > Java序列简单使用
Java序列简单使用
package javatest;import java.io.*;public class SerializableTest implements Serializable { public static class Test implements Serializable { private int width; private int height; public void setWidth(int width) { this.width = width; } public void setHeight(int height) { this.height = height; } } public static void write(Object obj, String path) { try { File f=new File(path); if(f.exists()){ f.delete(); } FileOutputStream fs = new FileOutputStream(path); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(obj); os.close(); } catch (Exception ex) { ex.printStackTrace(); } } public static Object read(String path) throws IOException, ClassNotFoundException { FileInputStream fs = new FileInputStream(path); ObjectInputStream os = new ObjectInputStream(fs); return os.readObject(); } public static void main(String[] args) throws ClassNotFoundException, IOException { Test test = new Test(); test.setWidth(50); test.setHeight(30); String path = "ser.file"; write(test, path); Test test1 = (Test)read(path); System.out.println(test1.height); }}
Java序列简单使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。