当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.io.RandomAccessFile.readDouble()用法及代码示例



描述

这个java.io.RandomAccessFile.readDouble()方法从此文件读取双精度值。此方法读取一个 long 值,从当前文件指针开始,就像通过 readLong 方法一样,然后使用 Double 类中的 longBitsToDouble 方法将该 long 转换为 double。

声明

以下是声明java.io.RandomAccessFile.readDouble()方法。

public final double readDouble()

参数

NA

返回值

此方法返回此文件的下八个字节,解释为双精度值。

异常

  • 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

相关用法


注:本文由纯净天空筛选整理自 Java.io.RandomAccessFile.readDouble() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。