オブジェクトフロー

27236 ワード

プログレッシブ:オブジェクト情報を格納または伝送可能な情報フォーマットオブジェクトストリームに変換する:プログレッシブ出力ストリームObject OutputStream逆プログレッシブ入力ストリームObject InputStream新規作成方法readXxx()writeXxx()
プロローグ後のアンチプロローグは、すべてのクラスがプログレッシブ化されていません。一つの空インターフェースjava.io.Serializableは、すべての属性がプログレッシブではなく、すべての属性が必要です。tranient静的な内容はプログレッシブではなく、デフォルト値は父がSerializableを実現すれば、サブクラスのすべての内容がプログレッシブ化されます。サブの内容だけあります。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 *    :
 * 1、     
 * 2、            
 * 3、             Serializable
 * 
 * ObjectOutputStream
 * ObjectInputStream
 * @author TW
 *
 */
public class ObjectTest {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		//   -->   
		ByteArrayOutputStream baos =new ByteArrayOutputStream();
		ObjectOutputStream oos =new ObjectOutputStream(new BufferedOutputStream(baos));
		//       +  
		oos.writeUTF("    ");
		oos.writeInt(18);
		oos.writeBoolean(false);
		oos.writeChar('a');
		//  
		oos.writeObject("     ");
		oos.writeObject(new Date());
		Employee emp =new Employee("  ",400);
		oos.writeObject(emp);
		oos.flush();
		byte[] datas =baos.toByteArray();
		System.out.println(datas.length);
		//   -->    
		ObjectInputStream ois =new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
		//       
		String msg = ois.readUTF(); 
		int age = ois.readInt();
		boolean flag = ois.readBoolean();
		char ch = ois.readChar();
		System.out.println(flag);
		//         
		Object str = ois.readObject();
		Object date = ois.readObject();
		Object employee = ois.readObject();
		
		if(str instanceof String) {
			String strObj = (String) str;
			System.out.println(strObj);
		}
		if(date instanceof Date) {
			Date dateObj = (Date) date;
			System.out.println(dateObj);
		}
		if(employee instanceof Employee) {
			Employee empObj = (Employee) employee;
			System.out.println(empObj.getName()+"-->"+empObj.getSalary());
		}
		
	}

}
//javabean     
class Employee implements java.io.Serializable{
	private transient String name; //         
	private double salary;
	public Employee() {
	}
	public Employee(String name, double salary) {
		this.name = name;
		this.salary = salary;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 *    : 1、      2、             3、             Serializable
 * 
 * ObjectOutputStream ObjectInputStream
 * 
 * @author TW
 *
 */
public class ObjectTest02 {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		//    -->   
		ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("obj.ser")));
		//        +  
		oos.writeUTF("     ");
		oos.writeInt(18);
		oos.writeBoolean(false);
		oos.writeChar('a');
		//   
		oos.writeObject("   ");
		oos.writeObject(new Date());
		Employee emp = new Employee("   ", 400);
		oos.writeObject(emp);
		oos.flush();
		oos.close();
		//    -->    
		ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("obj.ser")));
		//        
		String msg = ois.readUTF();
		int age = ois.readInt();
		boolean flag = ois.readBoolean();
		char ch = ois.readChar();
		System.out.println(flag);
		//        
		Object str = ois.readObject();
		Object date = ois.readObject();
		Object employee = ois.readObject();

		if (str instanceof String) {
			String strObj = (String) str;
			System.out.println(strObj);
		}
		if (date instanceof Date) {
			Date dateObj = (Date) date;
			System.out.println(dateObj);
		}
		if (employee instanceof Employee) {
			Employee empObj = (Employee) employee;
			System.out.println(empObj.getName() + "-->" + empObj.getSalary());
		}
		ois.close();
	}
}