gpt4 book ai didi

java - 反序列化为大小为 0 的数组

转载 作者:太空宇宙 更新时间:2023-11-04 10:28:08 24 4
gpt4 key购买 nike

在 Treets 类的 load() 方法中,我声明了一个大小为 0 的 Treet 对象数组。但是,在反序列化时,我将 readObject() 方法的结果保存到同一个数组中。碰巧我首先序列化了 2 个对象,所以反序列化时我也得到了 2 个对象。当我将 2 个对象插入先前初始化的大小为 0 的数组时,为什么没有收到 ArrayIndexOutOfBoundsException 异常?

Treet.java

import java.util.Date;
import java.io.Serializable;
public class Treet implements Comparable<Treet>, Serializable {
private String mAuthor;
private String mDescription;
private Date mCreationDate;
Treet(String author, String description, Date creationDate) {
mAuthor = author;
mDescription = description;
mCreationDate = creationDate;
}
public Date getCreationDate() {
return mCreationDate;
}
@Override
public String toString() {
return String.format("Treet by %s: \"%s\" on %s", mAuthor, mDescription, mCreationDate);
}
@Override
public int compareTo(Treet anotherTreet) {
if(equals(anotherTreet)) {
return 0;
}
return mCreationDate.compareTo(anotherTreet.getCreationDate());
}
}

Treets.java

import java.io.*;
public class Treets {
public static void save(Treet[] treets) {
try(
FileOutputStream fos = new FileOutputStream("treets.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
)
{
oos.writeObject(treets);
} catch(IOException ioe) {
System.out.println(ioe.getMessage());
}
}
public static Treet[] load() {
Treet[] treets = new Treet[0];
try(
FileInputStream fis = new FileInputStream("treets.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
)
{
treets = (Treet[])ois.readObject();
} catch(IOException ioe) {
System.out.println(ioe.getMessage());
} catch(ClassNotFoundException cnfe) {
System.out.println(cnfe.getMessage());
}
return treets;
}
}

Main.java

import java.util.Date;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Treet treetOne = new Treet("John", "This is treet one", new Date());
Treet treetTwo = new Treet("Jane", "This is treet two", new Date(1526074720000L));
Treet[] treets = {treetOne, treetTwo};
Arrays.sort(treets);
Treets.save(treets);
Treet[] loadedTreets = Treets.load();
for(Treet treet : loadedTreets) {
System.out.println(treet.toString());
}
}
}

最佳答案

您误解了 ObjectInputStream.read 的概念。

它不会填充您声明的数组,而是返回一个包含文件内容的新数组

//A
Treet[] treets = new Treet[0]; //at this moment you are declaring the variable and initializing it with an array of 0 spaces
try(
FileInputStream fis = new FileInputStream("treets.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
)
{ //B
treets = (Treet[])ois.readObject(); //at this moment you are replacing the pointer of the previous 0 length array to a new one read from the file

} catch(IOException ioe) {
System.out.println(ioe.getMessage());
} catch(ClassNotFoundException cnfe) {
System.out.println(cnfe.getMessage());
}
return treets;
}

换句话说:在“A”上,变量treets指向内存X中的一个地址......在“B”上,变量更改为指向内存 Y 中的新地址

程序结束后,X 的内容仍然是一个长度为零的数组,但没有变量指向它。

如果你想测试你可以这样做:

Treet[] treets = new Treet[0]; 
Treet[] treetsOld = treets;
try(
System.out.println(treetsOld == treets);
FileInputStream fis = new FileInputStream("treets.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
)
{
treets = (Treet[])ois.readObject();
System.out.println(treetsOld == treets);
} catch(IOException ioe) {
System.out.println(ioe.getMessage());
} catch(ClassNotFoundException cnfe) {
System.out.println(cnfe.getMessage());
}
return treets;
}

关于java - 反序列化为大小为 0 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50311360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com