I am using MappedByteBuffer to load a file I wrote into memory. This is supposed to be a fast way of loading a file. My file contains a bunch of ints and doubles. There are a lot of tutorials online that show you how to write and then read the file. This is my test:
private static int count = (500*4) + (500*8);
public static void main(String[] args) throws IOException {
    //writing two arrays to file
    int[] a = new int[500];
    double [] b = new double[500];
    for (int i = 0; i < a.length; i++){
        a[i] = i;
        b[i] = (double)i;
    }
    RandomAccessFile memoryMappedFile = new RandomAccessFile("f.txt","rw");
    MappedByteBuffer out = memoryMappedFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, count);
    for(int i = 0 ; i < a.length; i++) {
        out.putInt(a[i]);
        out.putDouble(b[i]);
    }
    //reading back the ints and doubles
    int c = 0;
    out.position(c);
    while (out.hasRemaining()){
        System.out.println(out.getInt(c));
        c += 4;
        out.position(c);
        System.out.println(out.getDouble(c));
        c += 8;
        out.position(c);
    }
}
This is all well and good but the problem with these tutorials is that they are only printing the contents of the file to console. However, If I want to use the values of the file to do more calculations, later on, I need to store them to some variables (or arrays), essentially making a new copy of them which defeats the purpose of the memory mapped file.
The only other workaround is to call out.position() on demand but I need to know at what position is my desired value I want to use which I think is impossible to know if I am not traversing them sequentially.
in c/c++ you can create an int variable that will point to the memory mapped int so You will not copy the value but in Java, this is not possible.
So Essentially I would like to access the memory mapped file in random access rather than sequential.
Is this maybe a disadvantage of using MemoryMappedFiles in java?
