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

📄 customserial.java

📁 Java就业的培训教程书籍
💻 JAVA
字号:
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;
	}
	private void writeObject(ObjectOutputStream outObj)throws IOException{
		outObj.writeUTF(type);
		outObj.writeUTF(price);
		outObj.writeObject(color);
		outObj.writeInt(size);
	}
	private void readObject(ObjectInputStream inObj)throws IOException{
		type = inObj.readUTF();
		price = inObj.readUTF();
		Object cObj;
		try{
			cObj = inObj.readObject();
			color = (Color)(cObj);
		}catch(ClassNotFoundException e){}
		size = inObj.readInt();
	}
}
public class CustomSerial{
	public static void main(String args[])throws IOException,ClassNotFoundException{
		Color cObj = Color.red;
		Bag bagObj = new Bag(cObj,"57",18,"Student bag");
		FileOutputStream fileOStream = new FileOutputStream("info.ser");
		ObjectOutputStream objOStream = new ObjectOutputStream(fileOStream);
		try
		{
			objOStream.writeObject(bagObj);
			objOStream.close();
		}catch(IOException e)
		{
			System.out.println(e);
		}
		bagObj = null;
		FileInputStream fileInStream = new FileInputStream("info.ser");
		ObjectInputStream objInStream = new ObjectInputStream(fileInStream);
		try
		{
			bagObj = (Bag)objInStream.readObject();
			objInStream.close();
		}catch(IOException e)
		{
			System.out.println(e);
		}
		System.out.println("*********************************");
		System.out.println("There are information about bag:");
		System.out.println("Color:"+bagObj.color);
		System.out.println("Price:"+bagObj.price);
		System.out.println("Size:"+bagObj.size);
		System.out.println("Type:"+bagObj.type);
	}
}

⌨️ 快捷键说明

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