⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectserialtest.java

📁 this is a java application
💻 JAVA
字号:
import java.io.*;

class ObjectSerialTest
{
	public static void main(String[] args) throws Exception
	{
		Employee e1=new Employee("zhangsan",25,3000.50);
		Employee e2=new Employee("lisi",24,3200.40);
		Employee e3=new Employee("wangwu",27,3800.55);
		
		FileOutputStream fos=new FileOutputStream("employee.txt");
		ObjectOutputStream oos=new ObjectOutputStream(fos);
		oos.writeObject(e1);
		oos.writeObject(e2);
		oos.writeObject(e3);
		oos.close();
		
		FileInputStream fis=new FileInputStream("employee.txt");
		ObjectInputStream ois=new ObjectInputStream(fis);
		Employee e;
		for(int i=0;i<3;i++)
		{
			e=(Employee)ois.readObject();
			System.out.println(e.name+":"+e.age+":"+e.salary);
		}
		ois.close();
	}
}

class Employee implements Serializable
{
	String name;
	int age;
	double salary;
	transient Thread t=new Thread();
	public Employee(String name,int age,double salary)
	{
		this.name=name;
		this.age=age;
		this.salary=salary;
	}
	private void writeObject(java.io.ObjectOutputStream oos) throws IOException
	{
		oos.writeInt(age);
		oos.writeUTF(name);
		System.out.println("Write Object");
	}
	private void readObject(java.io.ObjectInputStream ois) throws IOException
	{
		age=ois.readInt();
		name=ois.readUTF();
		System.out.println("Read Object");
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -