【发布时间】:2016-05-10 05:20:19
【问题描述】:
下面是我的程序,
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();
}
}
}
我想知道为什么第一个位置是 0,第二个值是 8,第四个值是 8 *3?这些数字对应什么?此外,当它向“随机”写入数据时,java 是否会创建一个名为 random 的文件?我没有创建文本文件,所以这个随机文件存储在哪里?
【问题讨论】:
标签: java eclipse random io randomaccessfile