【发布时间】:2020-07-08 09:06:10
【问题描述】:
我正在尝试用Java 制作一个小程序,我需要在其中保存一个文件中的对象数组。我一直在寻找如何做到这一点,我找到了下一个代码:
//this is the array that will be stored
private Atributtes list[]=new Atributtes [100];
/*
this is the way i save the data in the array,i always save them in
index 0,and i made a for to put away the previous data because always
there will have data in index 0
*/
for (int i = totalElments; i>0; i--){
list[i] = list[i-1];
}
list[0]=new Atributtes ("Atributtes data");
save.storeInfile(list);
totalElments++;
private static final String filename="file.obj";
public void storeInfile(Object array[]){
try{
FileOutputStream file= new FileOutputStream(filename,true);
ObjectOutputStream object= new ObjectOutputStream(file);
object.writeObject(array);
object.close();
file.close();
System.out.println("recording successfully");
System.out.println("-----------------------------------");
}catch(Exception exceccao){
System.out.println("recording wasn't successfull");
}
}
我现在的问题是,当我想保存相同的对象数组但使用另一个数据时,或者当我重新打开文件时,文件中的第一个数据被覆盖。
注意 1:据说我将真正的布尔值放在 FileoutPutSTream 上,但是当我把它放入时,录制不再发生,但是当我删除真正的布尔值时,录制会发生,但总是会覆盖数据。
注意 2:我已经阅读过 FileWriter 和 PrintWriter,但我只需要编写对象,而不是字符串。谢谢!!!!
有人能告诉我如何在文件中附加这些新数据吗?
谢谢。
【问题讨论】:
标签: java arrays file object append