ArrayList的Java序列化/反序列化仅在第一次程序执行时起作用
我尝试用一些对象序列化和反序列化ArrayList。我第一次运行程序时,一切正常,但下一次就不正常了:ArrayList的Java序列化/反序列化仅在第一次程序执行时起作用,java,arraylist,serialization,deserialization,Java,Arraylist,Serialization,Deserialization,我尝试用一些对象序列化和反序列化ArrayList。我第一次运行程序时,一切正常,但下一次就不正常了: public class Test { private static final String FILE_NAME = "Objects.ser"; public static void main(String[] args) { ArrayList<CustomObject> customObjects = getCustomObjects(
public class Test {
private static final String FILE_NAME = "Objects.ser";
public static void main(String[] args) {
ArrayList<CustomObject> customObjects = getCustomObjects();
System.out.println("CustomObjects count: "+customObjects.size());
System.out.println("Adding 5 CustomObjects");
Random rand = new Random();
for(int i=0; i<5; i++){
CustomObject obj = new CustomObject();
obj.setIntValue(rand.nextInt());
customObjects.add(obj);
}
System.out.println("CustomObjects count: "+customObjects.size());
System.out.println("Save and load CustomObjects");
saveCustomObjects(customObjects);
customObjects = getCustomObjects();
System.out.println("CustomObjects count: "+customObjects.size());
}
public static ArrayList<CustomObject> getCustomObjects(){
try (
FileInputStream fin = new FileInputStream(FILE_NAME);
ObjectInputStream ois = new ObjectInputStream(fin);
){
return (ArrayList<CustomObject>) ois.readObject();
} catch (Exception ex) {
return new ArrayList<>();
}
}
public static void saveCustomObjects(ArrayList<CustomObject> strategies) {
try(
FileOutputStream fout = new FileOutputStream(FILE_NAME, true);
ObjectOutputStream oos = new ObjectOutputStream(fout);
){
oos.writeObject(strategies);
//tried also with oos.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public class CustomObject implements Serializable{
static final long serialVersionUID = 42L;
private int intValue=0;
private EnumTypes enumType=EnumTypes.ENUM_TYPE_ONE;
public enum EnumTypes{
ENUM_TYPE_ONE, ENUM_TYPE_TWO
}
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public EnumTypes getEnumTypes() {
return enumType;
}
public void setEnumTypes(EnumTypes enumTypes) {
this.enumType = enumTypes;
}
@Override
public int hashCode() {
int hash = 3;
hash = 97 * hash + this.intValue;
hash = 97 * hash + Objects.hashCode(this.enumType);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final CustomObject other = (CustomObject) obj;
if (this.intValue != other.intValue) {
return false;
}
if (this.enumType != other.enumType) {
return false;
}
return true;
}
@Override
public String toString() {
return "CustomObject{" + "intValue=" + intValue + ", enumTypes=" + enumType + '}';
}
}
但在下一次运行后,输出看起来始终像是无法覆盖序列化ArrayList中包含对象的文件:
CustomObjects count: 5
Adding 5 CustomObjects
CustomObjects count: 10
Save and load CustomObjects
CustomObjects count: 5
我在mac上的netbeans和console中进行了测试。有人知道问题出在哪里吗?确切地说,没有任何内容会被覆盖,因为您在写入文件时明确选择了追加而不是覆盖:
new FileOutputStream(FILE_NAME, true);
因此,第一次运行不读取任何内容,并将包含5个元素的列表附加到文件中。第二次运行读取唯一列表并将另一个包含10个元素的列表附加到文件中。第三次运行读取文件中的第一个元素列表,并附加另一个包含10个元素的列表,以此类推
删除第二个参数,或将其设置为false。如果要覆盖该文件,请不要附加到该文件。非常感谢
new FileOutputStream(FILE_NAME, true);