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


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


io.PushbackReader Class in Java

java.io.PushbackReader 是一個字符流讀取器類,它允許將字符推回流中。

聲明:

public class PushbackReader
   extends FilterReader

構造函數:

  • PushbackReader(閱讀器推送):創建一個新的推回讀取器 - “push” 並帶有字符推回緩衝區。
  • PushbackReader(讀卡器推送,int大小):創建一個具有特定大小的推回緩衝區的新推回讀取器。

PushbackReader類的方法:

  • read():java.io.PushbackReader.read()讀取單個字符。
    Syntax :
    public int read()
    Parameters :
    -----------
    Return : 
    reads single character from the Pushback buffer 
    else, -1 i.e. when end of file is reached.
    Exception :
    ->  IOException : If I/O error occurs.
  • 讀取(char[] carray,int 偏移量,int maxlen):java.io.PushbackReader.read(char[] carray, int offset, int maxlen)讀取字符數組的某些部分
    Syntax :
    public int read(char[] carray, int offset, int maxlen)
    Parameters :
    carray : destination buffer to be read into
    offset : starting position of the carray
    maxlen : maximum no. of characters to be read
    Return : 
    reads some portion of the character array
    else, -1 i.e. when end of file is reached.
    Exception :
    ->  IOException : If I/O error occurs.
  • close():java.io.PushbackReader.close()關閉 PushbackReader 並將與此流關聯的係統資源釋放到垃圾Collector。
    Syntax :
    public void close()
    Parameters :
    ------
    Return : 
    void
    Exception :
    ->  IOException : If I/O error occurs.
  • 標記(int arg):java.io.PushbackReader.mark(int arg)標記 PushbackReader 的當前位置。對於 PushbackReader,此方法總是拋出異常。
    Syntax :
    public void mark(int arg)
    Parameters :
    arg : integer specifying the read limit of the Stream
    Return : 
    void
  • 跳過(長字符):java.io.PushbackReader.skip(長字符)跳過並丟棄‘char’號。 PushbackReader 數據中的字符數。
    Syntax :
    public long skip(long char)
    Parameters : 
    char : no. of bytes of PushbackReader data to skip.
    Return : 
    no. of bytes to be skipped
    Exception: 
    -> IOException : in case I/O error occurs
  • reset():java.io.PushbackReader.reset()由mark()方法調用。它將 PushbackReader 重新定位到標記位置。對於 PushbackReader,此方法總是拋出異常。
    Syntax :
    public void reset()
    Parameters :
    ----
    Return : 
    void
    Exception :
    ->  IOException : If I/O error occurs.
    
  • markSupported():java.io.PushbackReader.markSupported()告知此流是否支持 mark() 操作,但它不支持。
    Syntax :
    public boolean markSupported()
    Parameters :
    -------
    Return : 
    true if PushbackReader supports the mark() method  else,false
  • ready():java.io.PushbackReader.ready()告知流是否已準備好讀取
    Syntax :
    public boolean ready()
    Parameters :
    -------
    Return : 
    true if the stream is ready to be read else,false
  • 未讀(int char):java.io.PushbackReader.unread(int char)將單個字符推入推回緩衝區。在此方法返回後,要讀取的下一個字符將具有 (char)char 值。

    用法:

    public void unread(int char)
    Parameters :
    char : int value of the character to be pushed back
    Return : 
    void
    Exception :
    ->  IOException : If I/O error occurs or Pushback buffer is full.
    
  • 未讀(char[] carray):java.io.PushbackReader.unread(char[] carray)將字符數組推送到 Pushback 緩衝區。被壓入的數組的索引從 0 開始。
    Syntax :
    public void unread(char[] carray)
    Parameters :
    carray : character array to be pushed back
    Return : 
    void
    Exception :
    ->  IOException : If I/O error occurs or Pushback buffer is full.
    
  • 解釋PushbackReader方法的Java代碼:read(char[] carray)、close()、markSupported()、read()、mark()、ready()、skip()、unread()

    
    // Java program illustrating the working of PushbackReader 
    // read(char[] carray), close(), markSupported() 
    // read(), mark(), ready(), skip(), unread() 
      
    import java.io.*; 
    public class NewClass 
    { 
        public static void main(String[] args) throws IOException 
        { 
            try
            { 
                // Initializing a StringReader and PushbackReader 
                String s = "GeeksForGeeks"; 
      
                StringReader str_reader = new StringReader(s); 
                PushbackReader geek_pushReader1 = new PushbackReader(str_reader); 
                PushbackReader geek_pushReader2 = new PushbackReader(str_reader); 
      
                // Use of ready() method : 
                System.out.println("Is stream1 ready : " + geek_pushReader1.ready()); 
                System.out.println("Is stream2 ready : " + geek_pushReader2.ready()); 
      
                // Use of read() : 
                System.out.println("\nWe have used skip() method in 1 : "); 
                System.out.print("\nUse of read() in 1 : "); 
                for (int i = 0; i < 6; i++) 
                { 
                    char c = (char) geek_pushReader1.read(); 
                    System.out.print(c); 
      
                    // Use of skip() : 
                    geek_pushReader1.skip(1); 
                } 
                System.out.println(""); 
      
                // USing read() : 
                char[] carray = new char[20]; 
                System.out.println("Using read() in 2 : " + geek_pushReader2.read(carray)); 
      
      
                // USe of markSupported() : 
       System.out.println("\nIs mark supported in 1  : " + geek_pushReader1.markSupported()); 
      
                geek_pushReader2.unread('F'); 
      
                // read the next char, which is the one we unread 
                char c3 = (char) geek_pushReader2.read(); 
                System.out.println("USe of unread() : " + c3); 
      
                // USe of mark() : 
                geek_pushReader1.mark(5); 
      
                // Use of close() : 
                geek_pushReader1.close(); 
      
            } 
            catch (IOException excpt) 
            { 
                System.out.println("mark not supported in 1"); 
      
            } 
        } 
    } 

    輸出:

Is stream1 ready : true
Is stream2 ready : true

We have used skip() method in 1 : 

Use of read() in 1 : GesoGe
Using read() in 2 : 1

Is mark supported in 1 : false
USe of unread() : F
mark not supported in 1


相關用法


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