是否可以随机访问存储在Java文件中的序列化数组中的特定索引值?

时间:2017-08-16 17:05:09

标签: java serialization

我正在尝试访问序列化数组对象文件中的数组索引位置。我不想反序列化整个数组对象,然后访问该数组。我已经能够使用seekg在C ++中完成它,但在Java中却没有成功。

我已尝试使用RandomAccessFile [请参阅下面的代码],但我无法访问数组中的特定位置并将其转换为实际值[请参阅代码注释 - //随机访问阅读(阅读具体位置) )]。 让我知道这个问题的解决方案。

代码 -

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.Arrays;

class Bitmaps implements Serializable
{
    int bitmapArray[] = new int[5];

    @Override
    public String toString() {
        return "Bitmaps [bitmapArray=" + Arrays.toString(bitmapArray) + "]";
    }
}
public class RandomAccess2 {

    public static void main(String[] args) {

        try {
            Bitmaps bitmaps = new Bitmaps();

            String filename = "blocksinfo.txt";
            String workingDirectory = System.getProperty("user.dir");
            String FILEPATH = workingDirectory + "\\" + filename;  //C:\\Users\ashis\workspace\hackerrank1\src\hackerrank1\blocksinfo.txt';

            //Initialize array with 100....104
            int cnt=100;
            for(int i=0;i<5;i++)
                bitmaps.bitmapArray[i]=cnt++;


            //Serialize array and store in a file
            serializeObject(bitmaps, FILEPATH);
            int objLength = sizeof(bitmaps);  //function to calculate size of serialized array
            System.out.println("Length in bytes after serialization=>"+objLength);


            //System.out.println(new String(readFromFile(FILEPATH, 0, ewahBitmap1.sizeInBytes())));

            try {

                //Random Access Reading (Read all contents)
                byte[] byteArray = readFromFile(FILEPATH, 0, objLength);
                ByteArrayInputStream in = new ByteArrayInputStream(byteArray);
                ObjectInputStream is = new ObjectInputStream(in);
                Bitmaps bitmaps2 = (Bitmaps) is.readObject();
                System.out.println("Full Random Access Reading=>"+bitmaps2);


                //Random Access Reading (Read specific position)
                byte[] byteArray1 = readFromFile(FILEPATH, 98, 2);
                ByteArrayInputStream in1 = new ByteArrayInputStream(byteArray1);
                ObjectInputStream is1 = new ObjectInputStream(in1);
                int val = (int) is1.readObject();
                System.out.println("Partial Random Access Reading=>"+val);

            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            //writeToFile(FILEPATH, ewahBitmap1.toString(), 0);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static byte[] readFromFile(String filePath, int position, int size)
            throws IOException {

        RandomAccessFile file = new RandomAccessFile(filePath, "r");
        file.seek(position);
        byte[] bytes = new byte[size];
        file.read(bytes);
        file.close();
        return bytes;

    }

    public static void serializeObject(Object bitmap, String fileName) throws IOException
    {
        FileOutputStream fos = new FileOutputStream(fileName);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(bitmap);
        oos.close();

    }

    public static int sizeof(Object obj) throws IOException {
        ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream);

        objectOutputStream.writeObject(obj);
        objectOutputStream.flush();
        objectOutputStream.close();

        return byteOutputStream.toByteArray().length;
    }
}

1 个答案:

答案 0 :(得分:1)

不,这是不可能的。它不是随机访问格式。您必须反序列化数组,然后将其索引到内存中。