我尝试使用某些对象对ArrayList进行序列化和反序列化。第一次运行程序时,一切正常,但是下次运行时,则无效:
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 + '}';
}
}
该应用程序首次运行的输出显示了所有与预期类似的内容:
CustomObjects count: 0
Adding 5 CustomObjects
CustomObjects count: 5
Save and load CustomObjects
CustomObjects count: 5
但是在下一次运行之后,输出看起来总是像带有序列化ArrayList中对象的文件无法被覆盖:
CustomObjects count: 5
Adding 5 CustomObjects
CustomObjects count: 10
Save and load CustomObjects
CustomObjects count: 5
我在Mac上的netbeans和控制台中进行了测试。有人知道问题出在哪里吗?
答案 0 :(得分:3)
就是这样,什么也不会被覆盖,因为您在写入文件时明确选择了附加而不是覆盖:
new FileOutputStream(FILE_NAME, true);
因此,第一次运行不会读取任何内容,并将5个元素的列表附加到文件中。第二次运行读取唯一列表,并将另一个包含10个元素的列表追加到文件中。第三次运行读取文件中的第一个列表,并追加另一个包含10个元素的列表,依此类推。
删除第二个参数,或将其设置为false。