[英]Java Serializable EOF
EDITED So I'm trying to save in a .ser file objects of type Product( containing name, quantity, price ) as it follows: each time I add a new product I add it in the file. 已编辑,因此我尝试将.Product类型的对象保存在.ser文件中,其中包含以下名称:每次添加新产品时,我都会将其添加到文件中。 Then, each time I launch a window, I want to read these products I previously saved, in a TreeSet by calling the following method in the window's constructor:
然后,每次启动窗口时,我想通过在窗口的构造函数中调用以下方法来读取以前保存在TreeSet中的这些产品:
public void updateCurrentProducts()
{
Product p=null;
System.out.println("Updating the tree of products.");
try
{
FileInputStream fileIn = new FileInputStream("products.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
int c;
// if((c=in.read()) != -1)
{
// System.out.println("Not eof yet.");
while ((p = (Product) in.readObject()) != null)
{
addProduct(p);
System.out.println("Just found "+p.name+".");
}
}
in.close();
fileIn.close();
}catch(IOException ix)
{
ix.printStackTrace();
return;
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is how I add products: 这是我添加产品的方式:
public void addProduct(Product p)
{
System.out.println("Succesfully added new peoduct.");
FileOutputStream fileOut;
try
{
fileOut = new FileOutputStream("products.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(p);
System.out.println("Just saved "+p.name);
productTree.add(p);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in products.ser");
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
However I get java.io.EOFException
and I'm not sure why, since I have a condition not to access/save an elem unless ((p = (Product) in.readObject()) != null)
. 但是我得到
java.io.EOFException
,我不确定为什么,因为我有一个条件,除非((p = (Product) in.readObject()) != null)
否则我不能访问/保存一个elem。 I'm not sure how to avoid EOF. 我不确定如何避免EOF。 I tried with
read()
check, but it would simply avoid the problem, not help me save my changes. 我尝试使用
read()
检查,但是这样做只会避免问题,无法帮助我保存更改。 Also, I noticed only the last element I submit is saved. 另外,我注意到仅保存了我提交的最后一个元素。 Any suggestions?
有什么建议么?
EDIT After implementing the suggestion, my error switched to java.io.StreamCorruptedException: invalid type code: AC
. 编辑实施建议后,我的错误切换到
java.io.StreamCorruptedException: invalid type code: AC
。
I read this https://stackoverflow.com/a/2395269/4180889 but it's not clear for me what I should do if I want to keep the content. 我读到了这个https://stackoverflow.com/a/2395269/4180889,但是对于我来说,如果要保留内容我该怎么办还不清楚。
I noticed only the last element I submit is saved.
我注意到只有我提交的最后一个元素被保存。
Its because the write operations is done on truncate mode. 这是因为写操作是在截断模式下完成的。
fileOut = new FileOutputStream("products.ser");
Use true
flag for append mode: 对附加模式使用
true
标志:
fileOut = new FileOutputStream("products.ser", true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.