I/O(输入/输出)

1.创建引用
ObjectInputStream ois =null; ObjectOutputStream oos = null; ByteArrayInputStream bais = null; ByteArrayOutputStream baos = null;

2.引用指向实例对象

baos = new ByteArrayOutputStream();

3.i/o输入/输出
使用
try{}catch(){

}
最后注意:关闭输入输出流
4.几个输入输出重要的方法
write();
read();
close();
flush();
/**
     * 通过序列化和反序列的方式对对象进行深度复制
     */
    public static Object deeplyCopy(Serializable source){
    	
		if(source == null) {
			return null;
		}
    	//1.声明一个变量用来保存复制得到的目标对象
    	Object targetObject = null;
    	//2.声明四个变量用来保存四个流
    	ObjectInputStream ois =null;
    	ObjectOutputStream oos = null;
    	ByteArrayInputStream bais = null;
    	ByteArrayOutputStream baos = null;
    	//3.try...catch...finally结构
    	try{
    		//4.创建字节数组输出流
    		baos = new ByteArrayOutputStream();
    		//5.根据字节数组输出流创建对象输出流
    		oos = new ObjectOutputStream(baos);
    		//6.执行对象的序列化操作(本质:将对象序列化后得到的数据写入字节数组)
    		oos.writeObject(source);
    		//7.获取保存了序列化数据的字节数组
    		byte[] byteArray = baos.toByteArray();
    		//8.创建字节数组输入流
    		bais = new ByteArrayInputStream(byteArray);
    		//9.根据字节数组输入流创建对象输入流
    		ois = new ObjectInputStream(bais);
    		//10.执行反序列化操作
    		 targetObject = ois.readObject();
    	}catch(Exception e){
    		e.printStackTrace();
    	} finally{
    		//11.释放资源
    		if(oos != null){
    			try{
    				oos.close();
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    		if(ois != null){
    			try{
    				ois.close();
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    	}  	
    	return targetObject;
	}

  

原文地址:https://www.cnblogs.com/limingxian537423/p/7464734.html