Java Java类
这个类基本上是一个管道字符输出流。在 I/O Piped 中,仅表示同时在 JVM 中运行的两个线程之间的链接。因此,管道既可用作源,也可用作目标。
如果向连接的管道输出流提供数据字节的线程不再活动,则称该管道已损坏。
宣言:
public class PipedWriter
extends Writer
构造函数:
- PipedWriter() :创建一个未连接的 PipedWriter。
- PipedWriter(PipedReader inStream) :创建一个 PipedWriter,它连接到 PipedReader – 'inStream'。
方法:
- write(int char) : Java.io.PipedWriter.write(int char)将指定字符写入 PipedWriter。
句法 :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 offset, int maxlen) : Java.io.PipedWriter.write(char[] carray, int offset, int maxlen)将 maxlen字符从 'carray' 写入 PipedWriter。如果没有字符要写入 Stream,则该方法会阻塞。
句法 :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 并释放分配的资源。
句法 :public void close() Parameters : -------------- Return : void Exception : -> IOException : if in case IO error occurs.
- connect(PipedReader destination) : Java.io.PipedWriter.connect(PipedReader destination)将 PipedWriter 连接到“目标” PipedReader,如果“目标”是带有其他流的管道,则会引发 IO 异常
句法 :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()刷新输出流。
句法 :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 Java类