📜  Java.io.PipedReader Java中的类

📅  最后修改于: 2022-05-13 01:54:40.476000             🧑  作者: Mango

Java.io.PipedReader Java中的类

Java 中的 io.PipedReader 类

这个类基本上是一个管道字符输入流。在 I/O Piped中,简单地表示同时运行在 JVM 中的两个线程之间的链接。因此,管道既可用作源,也可用作目标。
如果向连接的管道输出流提供数据字节的线程不再活动,则称该管道已损坏。

宣言:

public class PipedReader
  extends Reader

构造函数:

  • PipedReader() :创建一个未连接的 PipedReader()。
  • PipedReader(int pSize) :创建一个 PipedReader,它不与指定的管道大小连接。
  • PipedReader(PipedWriterStream src) :创建一个 PipedReader,它连接到 PipedWriterStream – 'src'。
  • PipedReader(PipedWriterStream src, int pSize) :创建一个 Piped Reader,它连接到具有指定管道大小的 Piped Writer。

方法:

  • read() : Java.io.PipedReader.read()从 PipedReader 读取下一个字符。此方法阻塞,直到字符可用。如果检测到流结束,或者抛出异常并且方法阻塞,则返回 -1
    句法() :
    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
  • read(char[] carray, int offset, int maxlen) : Java.io.PipedReader.read(char[] carray, int offset, int maxlen)从 PipedReader Stream 读取 maxlen 个字符到字符数组。如果到达 Stream 的末尾或抛出异常,该方法将阻塞。
    句法 :
    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 并释放分配的资源。
    句法 :
    public void close()
    Parameters : 
    --------------
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs.
  • connect(PipedWriter source) : Java.io.PipedReader.connect(PipedWriter source)将 PipedReader 连接到“源”管道写入器,如果“源”是带有其他流的管道,则会引发 IO 异常
    句法 :
    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()告诉流是否准备好被读取
    句法 :
    public boolean ready()
    Parameters : 
    --------------
    Return :                                               
    true : if the stream is ready to be read else, false
    Exception :
    -> IOException : if in case IO error occurs.

    说明 PipedReader 类方法工作的Java程序:

    // 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 Java类