当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。