课上补做-1
要求:
教材p300 Example10_13
- 在长虹电视后增加一台海尔电视,价格是你学号的后四位
- 提交运行结果截图
- 刻下推送代码到码云
import java.io.*;
public class Example10_13 {
public static void main(String args[]) {
TV changhong = new TV();
changhong.setName("长虹电视");
changhong.setPrice(5678);
TV haier = new TV();
haier.setName("海尔电视");
haier.setPrice(5337);
File file=new File("television.txt");
try{
FileOutputStream fileOut=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(fileOut);
objectOut.writeObject(changhong);
objectOut.close();
FileInputStream fileIn=new FileInputStream(file);
ObjectInputStream objectIn=new ObjectInputStream(fileIn);
TV xinfei=(TV)objectIn.readObject();
objectIn.close();
xinfei.setName("新飞电视");
xinfei.setPrice(6666);
System.out.println("changhong的名字:"+changhong.getName());
System.out.println("changhong的价格:"+changhong.getPrice());
System.out.println("haier的名字:"+haier.getName());
System.out.println("haier的价格:"+haier.getPrice());
System.out.println("xinfei的名字:"+xinfei.getName());
System.out.println("xinfei的价格:"+xinfei.getPrice());
}
catch(ClassNotFoundException event) {
System.out.println("不能读出对象");
}
catch(IOException event) {
System.out.println(event);
}
}
}
import java.io.*;
public class TV implements Serializable{
String name;
int price;
public void setName(String s) {
name=s;
}
public void setPrice(int n) {
price=n;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
}
代码链接
https://gitee.com/BESTI-IS-JAVA-2018/20165337/tree/master/week11buzuo