我有一个文件example.dat,其中存储了不同的对象,假设类“ Example”的10个实例。 现在,在给定的时间,我需要读取一个特定的Example对象,比方说,我先读取第5个,然后读取第3个,然后读取第8个,依此类推。基本上,我想从该文件中读取它就像是一个Array。有什么办法可以实现? 现在,我有:
public static Example loadExampleFromFile(int index){
ObjectInputStream exampleStream = null;
try{
exampleStream = new ObjectInputStream(new FileInputStream("Example.dat"));
//Now I should point in the file at the beginning of the Example object
//which is the index-th object stored into the file
return (Example) exampleStream.readObject();
关于如何实现这一点的任何建议,而不必每次都遍历整个文件? 希望这很清楚。