Java输入输出流练习5-3,烦请老师检查和指正~
相关截图:

老师,请问为什么这里一直报错啊?我用的是英文输入法呀~
package com.imooc.input_output_exercise5_3;
import java.io.Serializable;
public class Product implements Serializable {
private int id;
private String name;
private String categories;
private double price;
public Product() {
}
public Product(int id, String name, String categories, double price) {
this.setId(id);
this.setName(name);
this.setCategories(categories);
this.setPrice(price);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategories() {
return categories;
}
public void setCategories(String categories) {
this.categories = categories;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", categories=" + categories + ", price=" + price + "]";
}
}
package com.imooc.input_output_exercise5_3;
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 TestProduct {
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("product.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream("product.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
oos.writeObject(iphone);
oos.writeObject(ipad);
oos.writeObject(macbook);
oos.writeObject(iwatch);
oos.writeObject(null);
oos.flush();
System.out.println("Apple Products Introduction:");
Product products;
while ((products = (Product) ois.readObject()) != null) {
System.out.println(products);
}
fos.close();
oos.close();
fis.close();
ois.close();
}catch(
FileNotFoundException e1)
{
e1.printStackTrace();
}catch(
ClassNotFoundException e2)
{
e2.printStackTrace();
}catch(
IOException e)
{
e.printStackTrace();
}
}}24
收起
正在回答 回答被采纳积分+1
2023版Java工程师
- 参与学习 人
- 提交作业 8792 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程



恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星