import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Launch {
public static void main(String[] args) {
List<Bean> beanList = new ArrayList<Bean>();
for (int i = 0; i < 10; i++) {
Bean bean = new Bean();
bean.id = i;
beanList.add(bean);
}
System.out.println();
List<Bean> newList = null;
try {
ByteArrayOutputStream bo=new ByteArrayOutputStream();//1行
ObjectOutputStream os = new ObjectOutputStream(bo);
os.writeObject(beanList);//3行
ByteArrayInputStream bo1 = new ByteArrayInputStream(bo.toByteArray());//1行
ObjectInputStream os1 = new ObjectInputStream(bo1);
newList = (List<Bean>)os1.readObject();
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < beanList.size(); i++) {
Bean oldBean = beanList.get(i);
Bean newBean = newList.get(i);
System.out.println("old:" + oldBean + " new:" + newBean);
}
}
}
class Bean implements Serializable {
private static final long serialVersionUID = 4948388455081444528L;
public int id = 0;
}
JAVA_利用流克隆List
最新推荐文章于 2023-08-06 23:03:22 发布