0

MappedByteBuffer を使用して、データをファイルに保存/ロードすることを考えています。long 型のフィールド A があり、シリアル化すると文字列のフィールド B が次のようになるとします。B(文字列)

今、私はそれに書いて読みたいと思っています。サンプルコードの一部を次に示します。

RandomAccessFile file = new RandomAccessFile(dataPath.toString(), "rw");
    MappedByteBuffer mbb = file.getChannel().map(FileChannel.MapMode
            .READ_WRITE, 0, 256);
    long num = 500;
    mbb.putLong(0, num); // (1) first write the long value at beginning
    String str = "Hello World!";
    byte[] input = str.getBytes();
    //then write a string
    mbb.put(input, 8, input.length); // (2) IndexOutOfBoundsException

mbb.getLong(0)
そのため、後で呼び出してlong を取得できますmbb.get(outputArray,8,outputArray.length)

しかし今、私は場所(2)で失敗しています。助言がありますか?

4

1 に答える 1