Java.io.RandomAccessFile.readDouble() 方法
描述
java.io.RandomAccessFile.readDouble() 方法从这个文件中读取一个双精度。 此方法从当前文件指针开始读取一个 long 值,就像通过 readLong 方法一样,然后使用 Double 类中的 longBitsToDouble 方法将该 long 转换为 double。
声明
以下是 java.io.RandomAccessFile.readDouble() 方法的声明。
public final double readDouble()
参数
NA
返回值
此方法返回此文件的接下来的 8 个字节,解释为双精度。
异常
IOException − 如果发生 I/O 错误。如果已到达文件结尾,则不会抛出。
EOFException − 如果此文件在读取 8 个字节之前到达结尾。
示例
下面的例子展示了 java.io.RandomAccessFile.readDouble() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class RandomAccessFileDemo {
public static void main(String[] args) {
try {
double d = 1.5987475;
// create a new RandomAccessFile with filename test
RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");
// write something in the file
raf.writeDouble(765.497634);
// set the file pointer at 0 position
raf.seek(0);
// read double
System.out.println("" + raf.readDouble());
// set the file pointer at 0 position
raf.seek(0);
// write a double at the start
raf.writeDouble(d);
// set the file pointer at 0 position
raf.seek(0);
// read double
System.out.println("" + raf.readDouble());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
假设我们有一个文本文件c:/test.txt,其内容如下。 该文件将用作我们示例程序的输入 −
ABCDE
让我们编译并运行上面的程序,这将产生下面的结果 −
765.497634 1.5987475

