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

时间:2018-05-12 22:59:55

标签: java serialization

在Treets类的readObject()方法中,我声明了一个大小为0的Treet对象数组。但是,在反序列化时,我将ArrayIndexOutOfBoundsException方法的结果保存到同样的方法中阵列。发生我首先序列化2个对象,所以我在反序列化时也得到2个对象。为什么我没有得到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()); } } 异常,因为我将2个对象推入先前初始化的大小为0的数组?

Treet.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;
    }
}

Treets.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());
        }
    }
}

Main.java

<script id="fragment-shader" type="x-shader/x-fragment">

precision mediump float;

float myred = 1.0, mygreen = 0.0, myblue = 0.0;
myred = 
void main()
{   
    gl_FragColor = vec4(myred, mygreen, myblue, 1.0 );
}
</script>

1 个答案:

答案 0 :(得分:0)

您误解了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;
}