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


Java Java.io.LineNumberReader用法及代碼示例

跟蹤行號的緩衝 character-input 流。該類定義了方法 setLineNumber(int) 和 getLineNumber() 分別用於設置和獲取當前行號。

  • 默認情況下,行編號從 0 開始。讀取數據時,該數字在每個行終止符處遞增,並且可以通過調用 setLineNumber(int) 進行更改。
  • 但請注意,setLineNumber(int) 實際上並不更改流中的當前位置;而是更改了流中的當前位置。它僅更改getLineNumber()返回的值。
  • 一行被視為以換行符 (‘\n’)、回車符 (‘\r’) 或回車符後緊跟換行符之一終止。

構造函數:

  • LineNumberReader(讀入):使用默認的 input-buffer 大小創建新的 line-numbering 讀卡器。
  • LineNumberReader(讀取器,int sz):創建一個新的 line-numbering 讀取器,將字符讀取到給定大小的緩衝區中。

方法:

  • int getLineNumber():獲取當前行號。
    Syntax :public int getLineNumber()
    Returns:
    The current line number
  • 無效標記(int readAheadLimit):標記流中的當前位置。對 reset() 的後續調用將嘗試將流重新定位到該點,並且還將相應地重置行號。
    Syntax :public void mark(int readAheadLimit)
              throws IOException
    參數:
    readAheadLimit - Limit on the number of characters that may be read 
    while still preserving the mark. After reading this many characters, 
    attempting to reset the stream may fail.
    Throws:
    IOException
  • int read():讀取單個字符。行終止符被壓縮為單個換行符 (‘\n’)。每當讀取行終止符時,當前行號就會遞增。
    Syntax :public int read()
             throws IOException
    返回:
    The character read, or -1 if the end of the stream has been reached
    Throws:
    IOException
  • int 讀取(char[] cbuf,int 關閉,int len):將字符讀入數組的一部分。每當讀取行終止符時,當前行號就會遞增。
    Syntax :public int read(char[] cbuf,
           int off,
           int len)
             throws IOException
    參數:
    cbuf - Destination buffer
    off - Offset at which to start storing characters
    len - Maximum number of characters to read
    返回:
    The number of bytes read, or -1 if the end of the stream has already been reached
    Throws:
    IOException
  • 字符串readLine():讀取一行文本。每當讀取行終止符時,當前行號就會遞增。
    Syntax :public String readLine()
                    throws IOException
    Returns:
    A String containing the contents of the line, not including any line 
    termination characters, or null if the end of the stream has been reached
    Throws:
    IOException
  • 無效reset():將流重置為最新標記。
    Syntax :public void reset()
               throws IOException
    Throws:
    IOException
  • void setLineNumber(int lineNumber):設置當前行號。
    Syntax :public void setLineNumber(int lineNumber)
    參數:
    lineNumber - An int specifying the line number
  • 長跳過(長n):跳過字符。
    Syntax :public long skip(long n)
              throws IOException
    參數:
    n - The number of characters to skip
    返回:
    The number of characters actually skipped
    Throws:
    IOException
    IllegalArgumentException

程序:


//Java program demonstrating LineNumberReader methods 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.LineNumberReader; 
class LineNumberReaderDemo 
{ 
    public static void main(String[] args) throws IOException  
    { 
        FileReader fr = new FileReader("file.txt"); 
        LineNumberReader lnr = new LineNumberReader(fr); 
        char c[] = new char[20]; 
  
        //illustrating setLineNumber() 
        lnr.setLineNumber(0); 
          
        //illustrating set 
        System.out.println(lnr.getLineNumber()); 
          
        //illustrating markSupported() method 
        if(lnr.markSupported()) 
        { 
            System.out.println("mark() method is supported"); 
            //illustrating mark method 
            lnr.mark(100); 
        } 
          
        /*File Contents 
        * This is first line 
        this is second line 
        This is third line 
        */
          
        //skipping 19 characters 
        lnr.skip(19); 
  
        //illustrating ready() method 
        if(lnr.ready()) 
        { 
            //illustrating readLine() method 
            System.out.println(lnr.readLine()); 
  
            //illustrating read(char c[],int off,int len) 
            lnr.read(c); 
            for (int i = 0; i <20 ; i++) 
            { 
                System.out.print(c[i]); 
            } 
              
            //illustrating reset() method 
            lnr.reset(); 
              
            for (int i = 0; i <18 ; i++) 
            { 
                //illustrating read() method 
                System.out.print((char)lnr.read()); 
            } 
            int ch; 
              
            //illustrating read() method 
            System.out.println(lnr.readLine()); 
            while((ch = lnr.read())==1) 
                System.out.print((char)ch); 
        } 
          
        //close the stream 
        lnr.close(); 
    } 
} 

輸出:

0
mark() method is supported
this is second line
This is third line
This is first line


相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.LineNumberReader class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。