lundi 9 mai 2016

In RandomAccessFile why is 0 the first position and 8 the fourth?

Here is my program below,

public class RandomAccessDemo {
public static void main(String[] args) {
    double data[] = { 19.4, 10.1, 123.54, 33.0, 87.9, 74.25 };
    double d;
    // open and use a random access file
    try (RandomAccessFile raf = new RandomAccessFile("random", "rw")) {
        // write values to the file
        for (int i = 0; i < data.length; i++) {
            raf.writeDouble(data[i]);
        }
        // now read back specific values
        raf.seek(0);// seek to first double
        d = raf.readDouble();
        System.out.println("First Values is " + d);

        raf.seek(8);// seek to first double
        d = raf.readDouble();
        System.out.println("Second Values is " + d);

        raf.seek(8 * 3);// seek to first double
        d = raf.readDouble();
        System.out.println("Fourth Values is " + d);

        System.out.println();
        // Now read every other value
        System.out.println("Here is every other value:");
        for (int i = 0; i < data.length; i += 2) {
            raf.seek(8 * i);// seek to ith double
            d = raf.readDouble();
            System.out.println(d + " ");
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

I was wondering why is 0 the first position, 8 the second value and 8 *3 the fourth value? What do these numbers correspond to? Also when it writes data to "random" does java create a file called random? I did not create the text file so where is this random file stored?




Aucun commentaire:

Enregistrer un commentaire