如果我要用while來迴圈輸出.readObject那我的判斷條件要怎麼判斷??
package com.imooc.FileOjectInOut;
import java.io.Serializable;
public class Product implements Serializable
{
private int id;
private String name;
private String type;
private double price;
public Product()
{
}
public Product(int id, String name, String type, double price)
{
this.id = id;
this.name = name;
this.type = type;
this.price = 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 getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
public String toString()
{
return ("產品ID:"+this.getId()+"\n產品名稱:"+this.getName()+"\n產品屬性:"+this.getType()+"\n產品價格:"+this.getPrice());
}
}//主方法 test
package com.imooc.FileOjectInOutTest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.imooc.FileOjectInOut.Product;
import com.imooc.filemodel.OjectInOutStream;
public class Test
{
public static void main(String[] args)
{
Product product1 = new Product(123, "電腦", "電子", 5600);
Product product2 = new Product(12, "電", "電5子", 456);
Product product3 = new Product(1, "腦", "電", 123);
Product product4 = new Product(666, "電腦王", "子", 789);
try
{
FileOutputStream fos = new FileOutputStream("test\\product.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test\\product.txt"));
oos.writeObject(product1);
oos.writeObject(product2);
oos.writeObject(product3);
oos.writeObject(product4);
oos.flush();
Product product0 = new Product();
try
{
for (int n = 0; n < 4; n++)
{
product0 = (Product) ois.readObject();
System.out.println(product0);
}
} catch (ClassNotFoundException e)
{
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
oos.close();
fos.close();
ois.close();
} catch (FileNotFoundException e)
{
// TODO 自動產生的 catch 區塊
e.printStackTrace();
} catch (IOException e)
{
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
}
}最後輸出到控制台部份不知道while迴圈判斷怎使用
正在回答 回答被采纳积分+1
/*
* 若文件中有若干个Object对象,你用ObjectInputStream中的readObject()去读,何时判读到结尾?
* 方法之一:(常用的方法)将若干个对象(数量不定)都装入一个容器中(如:ArrayList之类),
* 然后将容器这一个对象写入就行了。读取时,只要读取一个对象(即容器对象)就行了。
*
* 方法之二:(若不想用容器),则由于数量不定,正是用EOFException来判断结束。
* 代码结构如下:(无论是readInt()读int,还是readObject()读对象)
* try{
* while(true) {
* Object o=ois.radObject();
* 处理已读出的对象o;
* }
* }
* catch(EOFException e){
* //已从流中读完。
* }
* finallly {
* 流的关闭。 } */
抱歉問題沒說清楚
我想問的是.readObject()方法有辦法向.read方法那樣,可以是用類似讀到最後有值可以判斷是否已經讀到最後一個了??類似下面的迴圈判斷
while((n=fis.read())!=-1)
{
System.out.print((char)n);}
相似问题
登录后可查看更多问答,登录/注册
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程

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