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


Java Java.io.PipedWriter用法及代码示例


io.PipedWriter Class in Java

这个类本质上是一个管道character-output流。在输入/输出中管道式,简单来说就是JVM中同时运行的两个线程之间的链接。因此,管道既可以用作源也可以用作目标。
如果向连接的管道输出流提供数据字节的线程不再活动,则称管道已损坏。

声明:

public class PipedWriter
  extends Writer

构造函数:

  • PipedWriter():创建一个 PipedWriter,它未连接。
  • PipedWriter(PipedReader inStream):创建一个 PipedWriter,它连接到 PipedReader - ‘inStream’。

方法:

  • 写(int char):java.io.PipedWriter.write(int char)将指定字符写入 PipedWriter。
    Syntax :
    public void write(int char)
    Parameters : 
    char : character to be written
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs.
  • write(char[] carray, int 偏移量, int maxlen):java.io.PipedWriter.write(char[] carray, int 偏移量, int maxlen)将 ‘carray’ 中的 maxlen 字符写入 PipedWriter。如果没有剩余字符要写入 Stream,则该方法将阻塞。
    Syntax :
    public void write(char[] carray, int offset, int maxlen)
    Parameters : 
    carray : data of the carray
    offset : starting in the destination array - 'carray'.
    maxlen : maximum length of array to be read
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs.

    执行:

    
    // Java program illustrating the working of PipedWriter 
    // write(char[] carray, int offset, int maxlen) 
      
    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); 
      
            char[] carray = {'J', 'A', 'V', 'A'}; 
      
            // Use of write(char[] carray, int offset, int maxlen) 
            geek_writer.write(carray, 0, 4); 
            int a = 5; 
            System.out.print("Use of write(carray, offset, maxlen) : "); 
            while(a>0) 
            { 
                System.out.print(" " + (char) geek_reader.read());           
            } 
            
        } 
    } 

    输出:

    Use of write(carray, offset, maxlen) :  J A V A
  • close():java.io.PipedWriter.close()关闭PipedWriter并释放分配的资源。
    Syntax :
    public void close()
    Parameters : 
    --------------
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs.
  • 连接(PipedReader 目的地):java.io.PipedWriter.connect(PipedReader 目的地)将 PipedWriter 连接到 ‘destination’ PipedReader,如果 ‘destination’ 是带有其他流的管道,则会引发 IO 异常
    Syntax :
    public void connect(PipedReader destination)
    Parameters : 
    destination : the PipedReader to be connected to
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs.
  • flush():java.io.PipedWriter.flush()刷新输出流。
    Syntax :
    public void flush()
    Parameters : 
    ------------
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs.

    说明 PipedWriter 类方法工作的 Java 代码:

    
    // Java program illustrating the working of PipedWriter 
    // write(), connect 
    // close(), flush() 
      
    import java.io.*; 
    public class NewClass 
    { 
        public static void main(String[] args) throws IOException 
        { 
            PipedReader geek_reader = new PipedReader(); 
            PipedWriter geek_writer = new PipedWriter(); 
            try
            { 
                // Use of connect() : connecting geek_reader with geek_writer 
                geek_reader.connect(geek_writer); 
      
                // Use of write(int byte) : 
                geek_writer.write(71); 
                geek_writer.write(69); 
                geek_writer.write(69); 
                geek_writer.write(75); 
                geek_writer.write(83); 
      
                // Use of flush() method : 
                geek_writer.flush(); 
                System.out.println("Use of flush() method : "); 
      
                int i = 5; 
                while(i > 0) 
                { 
                    System.out.print(" " + (char) geek_reader.read()); 
                    i--; 
                } 
      
                // USe of close() method : 
                System.out.println("\nClosing the Writer stream"); 
                geek_writer.close(); 
      
            } 
            catch (IOException excpt) 
            { 
                excpt.printStackTrace(); 
            } 
        } 
    } 

    输出:

    Use of flush() method : 
     G E E K S
    Closing the Writer stream

    下一篇:Java.io.PipedReader



相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.io.PipedWriter Class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。