IO流实现深克隆

@Test
	public void test2() throws Exception {
		Boy person = new Boy(18,"程柏薹");
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		ObjectOutputStream oos = new ObjectOutputStream(baos);
		oos.writeObject(person);
		
		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
		ObjectInputStream ois = new ObjectInputStream(bais);
		Boy p2 = (Boy) ois.readObject();
		System.out.println(person+","+person.hashCode());
		System.out.println(p2+","+p2.hashCode());
		
	}

 

你可能感兴趣的:(笔记)