當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java RandomAccessFile writeLong()用法及代碼示例

RandomAccessFile類writeLong()方法

  • writeLong() 方法可在java.io包。
  • writeLong() 方法用於將 long 值作為 8 字節直接寫入文件,無需任何轉換。
  • writeLong() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
  • writeLong() 方法可能在寫入 long 值時拋出異常。
    IOException:此異常在執行輸入/輸出操作時可能會拋出異常。

用法:

    public final void writeLong(long val);

參數:

  • long val– 表示要寫入的 long 值。

返回值:

這個方法的返回類型是void,它什麽都不返回。

例:

// Java program to demonstrate the example 
// of void writeLong(long val)a method of
// RandomAccessFile

import java.io.*;

class RAFWriteLong {
 public static void main(String[] args) throws Exception {
  long lo = 124587584 l;
  // Instantiate a random access file
  // object with file name and permissions
  RandomAccessFile ra_f = new RandomAccessFile("e:/includehelp.txt", "rw");

  // By using writeLong() method is to 
  // write a long value to the file
  ra_f.writeLong(lo);

  // by using seek() method is to 
  // set the current file indicator
  // from where read/write could 
  // start i.e. we set here 0 so reading
  // will be done from 0 till EOF
  ra_f.seek(0);

  // By using readLong() method is to 
  // read a long value from this file
  System.out.print(" ra_f.readLong():" + ra_f.readLong());

  // By using close() method isto
  // close this stream ran_f
  ra_f.close();
 }
}

輸出

 ra_f.readLong():124587584


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java RandomAccessFile writeLong() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。