Java.io.RandomAccessFile.readDouble() 方法



描述

java.io.RandomAccessFile.readDouble() 方法從此檔案中讀取一個雙精度浮點數。此方法從當前檔案指標開始讀取一個長整數值(如同 readLong 方法一樣),然後使用 Double 類中的 longBitsToDouble 方法將該長整數值轉換為雙精度浮點數。

宣告

以下是 java.io.RandomAccessFile.readDouble() 方法的宣告。

public final double readDouble()

引數

返回值

此方法返回此檔案的下一個八個位元組,解釋為雙精度浮點數。

異常

  • IOException − 如果發生 I/O 錯誤。如果達到檔案末尾則不會丟擲。

  • EOFException − 如果在讀取八個位元組之前此檔案到達末尾。

示例

以下示例演示了 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.htm
廣告