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


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

RandomAccessFile類readLine()方法

  • readLine() 方法可在java.io包。
  • readLine() 方法用於從此 RandomAccessFile 讀取數據行,並從當前文件指針開始從文件中連續讀取字節,直到它指示到達 End-Of-Line 或 End-Of-File。
  • readLine() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
  • readLine() 方法可能會在讀取數據行時拋出異常。
    IOException:此異常在執行輸入/輸出操作時可能會拋出異常。

用法:

    public final String readLine();

參數:

  • 它不接受任何參數。

返回值:

這個方法的返回類型是String,它返回此文件的下一行數據,否則在到達 EOF 時返回 null。

例:

// Java program to demonstrate the example 
// of String readLine() method of
// RandomAccessFile

import java.io.*;

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

  // By using writeFloat() method is to 
  // write float to the file
  ra_f.writeUTF("Welcome, in Java World!!\n");

  // Initially set the file pointer
  // is at 1 for reading the file
  ra_f.seek(1);

  // By using readLine() method is to 
  // read a line of data from this file

  String line = ra_f.readLine();
  System.out.println("ra_f.readLine():" + line);

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

輸出

ra_f.readLine():↓Welcome, in Java World!!


相關用法


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