defaultserial.java

来自「《Java程序设计》第6、7、8章的源代码。有需要者请」· Java 代码 · 共 50 行

JAVA
50
字号
import java.io.*;
import java.awt.Color;
class Bag implements Serializable{
	Color color;
	String price;
	int size;
	String type;
	public Bag(Color color,String price,int size,String type){
		this.color = color;
		this.price = price;
		this.size = size;
		this.type = type;
	}
}
public class DefaultSerial{
	public static void main(String args[])throws IOException,ClassNotFoundException{
		Color c = Color.red;
		Bag bag = new Bag(c,"57",18,"Student bag");
		FileOutputStream fileO = new FileOutputStream("info.ser");
		ObjectOutputStream OS = new ObjectOutputStream(fileO);
		try
		{
			OS.writeObject(bag);
			OS.close();
		}catch(IOException e)
		{
			System.out.println(e);
		}
		bag = null;
		FileInputStream fileI = new FileInputStream("info.ser");
		ObjectInputStream IS =null;
		IS = new ObjectInputStream(fileI);
		try
		{
			bag = (Bag)IS.readObject();
			IS.close();
		}catch(IOException e)
		{
			System.out.println(e);
		}
		System.out.println("*********************************");
		System.out.println("There are information about bag:");
		System.out.println("Color:"+bag.color);
		System.out.println("Price:"+bag.price);
		System.out.println("Size:"+bag.size);
		System.out.println("Type:"+bag.type);
		System.out.println("*********************************");
	}
}

⌨️ 快捷键说明

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