Java.io.RandomAccessFile.readLong() 方法



描述

java.io.RandomAccessFile.readLong() 方法從此檔案中讀取一個帶符號的 64 位整數。此方法從檔案讀取八個位元組,從當前檔案指標開始。

宣告

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

public final long readLong()

引數

返回值

此方法返回此檔案的接下來的八個位元組,解釋為一個長整數。

異常

  • IOException − 如果發生 I/O 錯誤。

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

示例

以下示例演示了 java.io.RandomAccessFile.readLong() 方法的用法。

package com.tutorialspoint;

import java.io.*;

public class RandomAccessFileDemo {
   public static void main(String[] args) {

      try {
         long l = 536475859696l;
         
         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

         // write something in the file
         raf.writeLong(l);

         // set the file pointer at 0 position
         raf.seek(0);

         // print the long
         System.out.println("" + raf.readLong());

         // set the file pointer at 0 position
         raf.seek(0);

         // write something in the file
         raf.writeLong(4876347485l);

         raf.seek(0);
         // print the long
         System.out.println("" + raf.readLong());
         
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

假設我們有一個文字檔案 c:/test.txt,其內容如下。此檔案將用作我們示例程式的輸入:

ABCDE  

讓我們編譯並執行上面的程式,這將產生以下結果:

536475859696
4876347485
java_io_randomaccessfile.htm
廣告
© . All rights reserved.