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

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

相关截图:

https://img1.sycdn.imooc.com//climg/61646d1f096c6a9312260552.jpg

老师,请问为什么这里一直报错啊?我用的是英文输入法呀~

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();
	}

}}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕阿园 2021-10-12 10:14:37

同学你好,这是因为同学的代码中没有main方法导致的

https://img1.sycdn.imooc.com//climg/6164ee5209890ee010700256.jpg

添加后测试代码完成的是可以的,很棒呐,继续加油

祝学习愉快~


  • 提问者 Heijyu #1

    老师我的还是不行啊,添加后不就成两个了嘛?为啥要加两个;啊?我的还是不行一直报错啊

    2021-10-12 17:05:37
  • 提问者 Heijyu #2

    好了好了,谢谢老师,我真是个马大哈😳

    2021-10-12 17:49:21
  • 好帮手慕阿园 回复 提问者 Heijyu #3

    同学你好,老师说的是添加main方法哦,而不是分号

    public static void main(String[] args) {}

    同学的代码中是没有main方法的,如下

    https://img1.sycdn.imooc.com//climg/6165542a0909172706690498.jpg

    https://img1.sycdn.imooc.com//climg/6165543d097b02f611000241.jpg

    祝学习愉快~

    2021-10-12 17:56:49
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师