oos.writeObject(null);这是添加一个对象的默认值来结束查找嘛?
package com.imooc.serial;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ProductTest {
public static void main(String[] args) {
Product iphone=new Product(123,"iphone","telephone",4888);
Product ipad=new Product(234,"ipad","computer",5088);
Product macbook=new Product(345,"macbook","computer",10688);
Product iwatch=new Product(256,"iwatch","watch",4799);
try {
FileOutputStream fos=new FileOutputStream("E:\\Project\\hello.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
FileInputStream fis=new FileInputStream("E:\\Project\\hello.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
oos.writeObject(iphone);
oos.writeObject(ipad);
oos.writeObject(macbook);
oos.writeObject(iwatch);
oos.writeObject(null);
oos.flush();
Product products;
System.out.println("apple系列产品信息:");
while((products=(Product)ois.readObject())!=null) {
System.out.println(products);
}
fos.close();
oos.close();
fis.close();
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星