CloneUtil类:
package com.bijian.study.clone; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; public class CloneUtil { public static void main(String[] args) { //创建一个对象 TemplateObject templateObject = new TemplateObject(); templateObject.setAge(10); templateObject.setName("test"); ArrayList<String> descriptions = new ArrayList<String>(); descriptions.add("是一个好人"); descriptions.add(new String("好姑娘")); descriptions.add(new String("硕士毕业")); descriptions.add(new String("白富美")); templateObject.setDescriptionList(descriptions); Style style = new Style(); style.setLength(1600); style.setType("欧美风"); Clothing clothing = new Clothing(); clothing.setColor("红色"); clothing.setSize(160); clothing.setStyle(style); templateObject.setClothing(clothing); System.out.println(templateObject); TemplateObject cloneTemplateObject = (TemplateObject) CloneUtil.getCloneObject(templateObject); System.out.println(cloneTemplateObject); System.out.println("templateObject==templateObject:" + (templateObject == cloneTemplateObject)); System.out.println("templateObject.equals(templateObject):" + (templateObject.equals(cloneTemplateObject))); System.out.println("templateObject.toString().equals(templateObject.toString()):" + (templateObject.toString().equals(cloneTemplateObject.toString()))); } // 克隆对象 public static Object getCloneObject(Object bean) { Object cloneBean = null; try { ByteArrayOutputStream byout = new ByteArrayOutputStream(); ObjectOutputStream obj = new ObjectOutputStream(byout); obj.writeObject(bean); ByteArrayInputStream byin = new ByteArrayInputStream(byout.toByteArray()); ObjectInputStream ins = new ObjectInputStream(byin); cloneBean = (Object) ins.readObject(); } catch (Exception ex) { System.err.print(ex); } return cloneBean; } }
TemplateObject类:
package com.bijian.study.clone; import java.io.Serializable; import java.util.ArrayList; public class TemplateObject implements Serializable { /** * */ private static final long serialVersionUID = 6790908190923407706L; private int age; private String name; private ArrayList<String> descriptionList; private Clothing clothing; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public ArrayList getDescriptionList() { return descriptionList; } public void setDescriptionList(ArrayList descriptionList) { this.descriptionList = descriptionList; } public Clothing getClothing() { return clothing; } public void setClothing(Clothing clothing) { this.clothing = clothing; } public String toString() { String baseInfo = String.valueOf(age) + name; String descriptions = ""; for(int i=0;i<descriptionList.size();i++) { descriptions += descriptionList.get(i).toString(); } return baseInfo + descriptions + clothing; } }
Clothing类:
package com.bijian.study.clone; import java.io.Serializable; public class Clothing implements Serializable { /** * */ private static final long serialVersionUID = -2876485615924815655L; private String color; private int size; private Style style; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public Style getStyle() { return style; } public void setStyle(Style style) { this.style = style; } public String toString() { return color + String.valueOf(size) + style; } }
Style类:
package com.bijian.study.clone; import java.io.Serializable; public class Style implements Serializable { /** * */ private static final long serialVersionUID = -7052770518598516803L; private String type; private int length; public String getType() { return type; } public void setType(String type) { this.type = type; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } public String toString() { return type + String.valueOf(length); } }
运行结果:
10test是一个好人好姑娘硕士毕业白富美红色160欧美风1600 10test是一个好人好姑娘硕士毕业白富美红色160欧美风1600 templateObject==templateObject:false templateObject.equals(templateObject):false templateObject.toString().equals(templateObject.toString()):true
相关推荐
ObjectInputStream 和 ObjectOutputStream 的作用是,对基本数据和对象进行序列化操作支持。本文给大家详细介绍了ObjectInputStream 和 ObjectOutputStream的相关知识,感兴趣的朋友一起学习吧
JavaIO对象流 FileInputStream 与 ObjectInputStream 和 ObjectOutputStream 的组合 奥利弗·缪尔
录入输出项目,帮助网友解决问题:1、界面的切换,2、对象序列化问题ObjectInputStream ObjectOutputStream 储存数据用的记事本系列化对象的方法
在该方法中,首先通过FileInputStream和ObjectInputStream读取源文件对象,然后通过ObjectOutputStream将读取的对象写入备份文件中。这个简单的示例代码展示了如何使用Java序列化来实现一个文件备份工具的简单功能,...
ObjectInputStream序列化实例,简单就学会,JAVA原来so easy
反序列化:ObjectInputStream ObjectOutputStream介绍 (序列化) 构造: ObjectOutputStream(OutputStream out) 参数可以传入这两个:FileOutputStream,BufferedOutputStream 关键方法: writeObject(Object obj) 将...
ObjectInputStream ObjectOutputStream ByteArrayInputStream ByteArrayOutputStream 【字符流】 Reader Writer InputStreamReader OutputStreamWriter BufferedReader BufferedWriter /PrintWriter FileReader ...
主要介绍了java序列化与ObjectOutputStream和ObjectInputStream的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
ObjectInputStream的用法---马克-to-win java视频的介绍对象输入字节流
通过将对象序列化,可以方便地实现对象的传输及保存。 在Java中提供有ObjectInputStream与ObjectOutputStream这两个类用于序列化对象的操作。这两个类是用于存储和独缺对象的输入输出流类,只要把对象中的所有...
loadContactsFromFile()方法使用FileInputStream和ObjectInputStream对象来读取文件中保存的Contact对象列表。如果读取失败,会抛出IOException和ClassNotFoundException异常,此时会调用saveContactsToFile()方法将...
} 由于定义了二个接口方法,方法参数分别是ObjectOutputStream和ObjectInputStream类型对象,因此这二个接口方法必须通过实现该接口
实现类的对象的序列化可以使用ObjectOutputStream,实现步骤如下: 创建ObjectOutputStream对象; 调用ObjectOutputStream的writeObject方法输出对象。 Java高级程序设计实战教程第五章-Java序列化机制全文共15页,...
程序使用了ObjectInPutStream和ObjectOutPutStream这两个文件输入输出流,利用这两个文件输入输出流,可以方便的直接将通讯记录对象直接写入文件中,另外为了方便添加和删除一条记录还使用了系统的对象数组类...
要串行化一个对象,必须与一定的对象输入/输出流联系起来,通过对象输出流...在ObjectInputStream中用readObject()方法可以直接读取一个对象,ObjectOutputStream中用writeObject()方法可以直接将对象保存到输出流中。
4.java.io包中的objectinputstream和objectoutputstream类主要用于对对象(Object)的读写。 5.android 中service的实现方法是:startservice和bindservice。 6.activity一般会重载7个方法用来维护其生命周期,除了...
在ObjectInputStream 和ObjectOutputStream的帮助下,我们可以轻松的实现序列化。 只要我们的class 实现了java.io.Serializable接口,就可以利用ObjectOutputStream的writeObject()方法将一个对象序列化;利用...
◦public abstract void writeObject(ObjectOutputStream out); ◦public abstract void readObject(ObjectInputStream in); 要熟悉对文件,特别是文本文件的操作 如果读取文本文件,推荐使用BufferedReader ...
java IO 字节流练习代码 FileInputStream和FileOutputStream BufferedInputStream 和 BufferedOutputStream DataInputStream 和 ...ObjectInputStream和ObjectOutputStream PrintStream PushbackInputStream