這個類本質上是一個管道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 Java.io.PipedWriter.close()用法及代碼示例
- Java Java.io.PipedWriter.connect()用法及代碼示例
- Java Java.io.PipedWriter.flush()用法及代碼示例
- Java Java.io.PipedWriter.write()用法及代碼示例
- Java Java.io.PipedInputStream.available()用法及代碼示例
- Java Java.io.PipedInputStream.close()用法及代碼示例
- Java Java.io.PipedInputStream.connect()用法及代碼示例
- Java Java.io.PipedInputStream.read()用法及代碼示例
- Java Java.io.PipedOutputStream.close()用法及代碼示例
- Java Java.io.PipedOutputStream.connect()用法及代碼示例
- Java Java.io.PipedOutputStream.flush()用法及代碼示例
- Java Java.io.PipedOutputStream.write()用法及代碼示例
- Java Java.io.PipedReader.close()用法及代碼示例
- Java Java.io.PipedReader.connect()用法及代碼示例
- Java Java.io.PipedReader.read()用法及代碼示例
- Java Java.io.PipedReader.ready()用法及代碼示例
- Java Java.io.PipedReader用法及代碼示例
- Java Java.io.PipedOutputStream用法及代碼示例
- Java Java.io.PipedInputStream用法及代碼示例
- Java Java.io.PrintStream.append()用法及代碼示例
- Java Java.io.PrintStream.checkError()用法及代碼示例
- Java Java.io.PrintStream.clearError()用法及代碼示例
- Java Java.io.PrintStream.close()用法及代碼示例
- Java Java.io.PrintStream.flush()用法及代碼示例
- Java Java.io.PrintStream.format()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.PipedWriter Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。