java.io.RandomAccessFile.readDouble() 方法读取这个文件一个double值。此方法读取一个long值,并从当前文件指针,就好像readLong方法,然后用longBitsToDouble方法将转换为double。
声明
以下是java.io.RandomAccessFile.readDouble()方法的声明
public final double readDouble()
参数
-
NA
返回值
此方法返回这个文件的下八个字节,解释为一个double。
异常
-
IOException -- 如果发生I/ O错误。如果已经达到文件结束不会抛出。
-
EOFException -- 如果这个文件到达末尾之前读取8个字节。
例子
下面的示例演示java.io.RandomAccessFile.readDouble()方法的用法。
package com.yiibai; 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