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


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


io.PipedReader Class in Java

這個類本質上是一個管道character-input流。在I/O中管道式,簡單來說就是JVM中同時運行的兩個線程之間的鏈接。因此,管道既可以用作源也可以用作目標。
如果向連接的管道輸出流提供數據字節的線程不再活動,則稱管道已損壞。

聲明:

public class PipedReader
  extends Reader

構造函數:

  • PipedReader():創建一個PipedReader(),表明它未連接。
  • PipedReader(int pSize):創建一個 PipedReader,它未與指定的管道大小連接。
  • PipedReader(PipedWriterStream src):創建一個 PipedReader,它連接到 PipedWriterStream - ‘src’。
  • PipedReader(PipedWriterStream src,int pSize):創建一個連接到具有指定管道大小的管道寫入器的管道讀取器。

方法:

  • read():java.io.PipedReader.read()從 PipedReader 讀取下一個字符。此方法會阻塞,直到字符可用為止。如果檢測到流結束,或者拋出異常且方法阻塞,則返回 -1
    Syntax():
    public int read()
    Parameters: 
    -----------
    Return :
    reads the next character from PipedReader.
    else, return-1 if end of the stream is detected.
    Exception : 
    -> IOException : if in case an IO error occurs.
    

    執行:

    
    // Java program illustrating the working of read() method 
      
    import java.io.*; 
    public class NewClass 
    { 
        public static void main(String[] args) throws IOException 
        { 
            PipedReader geek_reader = new PipedReader(); 
            PipedWriter geek_writer = new PipedWriter(); 
              
                geek_reader.connect(geek_writer); 
      
                // Use of read() method  
                geek_writer.write(71); 
                System.out.println("using read() : " + (char)geek_reader.read()); 
                geek_writer.write(69); 
                System.out.println("using read() : " + (char)geek_reader.read()); 
                geek_writer.write(75); 
                System.out.println("using read() : " + (char)geek_reader.read());         
        } 
    } 

    輸出:

    using read() : G
    using read() : E
    using read() : K
  • 讀取(char[] carray,int 偏移量,int maxlen):java.io.PipedReader.read(char[] carray, int 偏移量, int maxlen)從 PipedReader 流讀取最多 maxlen 個字符到字符數組。如果到達 Stream 末尾或引發異常,該方法將阻塞。
    Syntax :
    public int read(char[] carray, int offset, int maxlen)
    Parameters : 
    carray : buffer into which the data is to be read
    offset : starting in the destination array - 'carray'.
    maxlen : maximum length of array to be read
    Return :                                               
    next 'maxlen' bytes of the data as an integer value 
    return -1 is end of stream is reached
    Exception :
    -> IOException : if in case IO error occurs.
    
  • close():java.io.PipedPipedReader.close()關閉 PipedReader Stream 並釋放分配的資源。
    Syntax :
    public void close()
    Parameters : 
    --------------
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs.
  • 連接(PipedWriter 源):java.io.PipedReader.connect(PipedWriter源)將 PipedReader 連接到 ‘source’ 管道寫入器,如果 ‘source’ 是帶有其他流的管道,則會引發 IO 異常
    Syntax :
    public void connect(PipedWriter source)
    Parameters : 
    source : the PipedWriter to be connected to
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs.
  • ready():java.io.PipedPipedReader.ready()告知流是否已準備好讀取
    Syntax :
    public boolean ready()
    Parameters : 
    --------------
    Return :                                               
    true : if the stream is ready to be read else, false
    Exception :
    -> IOException : if in case IO error occurs.

    Java 程序說明了 PipedReader 類方法的用法原理:

    
    // Java program illustrating the working of PipedReader 
    // connect(), read(char[] carray, int offset, int maxlen), 
    // close(), ready() 
      
    import java.io.*; 
    public class NewClass 
    { 
        public static void main(String[] args) throws IOException 
        { 
            PipedReader geek_reader = new PipedReader(); 
            PipedWriter geek_writer = new PipedWriter(); 
      
            // Use of connect() : connecting geek_reader with geek_writer 
            geek_reader.connect(geek_writer); 
      
            geek_writer.write(71); 
            geek_writer.write(69); 
            geek_writer.write(69); 
            geek_writer.write(75); 
            geek_writer.write(83); 
      
            // Use of ready() method 
            System.out.print("Stream is ready to be read : "+geek_reader.ready()); 
      
            // Use of read(char[] carray, int offset, int maxlen) 
            System.out.print("\nUse of read(carray, offset, maxlen) : "); 
            char[] carray = new char[5]; 
            geek_reader.read(carray, 0, 5); 
      
            for (int i = 0; i < 5; i++) 
            { 
                System.out.print(carray[i]); 
            } 
      
            // USe of close() method : 
            System.out.println("\nClosing the stream"); 
            geek_reader.close(); 
        } 
    } 

    輸出:

    Stream is ready to be read : true
    Use of read(carray, offset, maxlen) : GEEKS
    Closing the stream
  • 下一篇: Java.io.PipedWriter



相關用法


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