📄 defaultserial.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;
}
}
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -