?? pipe.java
字號(hào):
package PipeKwic;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
/**
* 一個(gè)管道對(duì)象將兩個(gè)讀寫(xiě)數(shù)據(jù)流連接起來(lái),寫(xiě)數(shù)據(jù)流可以向管道中寫(xiě)入數(shù)據(jù),
* 讀數(shù)據(jù)流則可以在管道中有可用數(shù)據(jù)的時(shí)候讀出數(shù)據(jù)
*/
public class Pipe {
private PipedWriter pipedWriter;
private PipedReader pipedReader;
/**
* 默認(rèn)構(gòu)造器,初始化讀和寫(xiě)管道,并建立它們之間的連接
* @throws IOException 如不能建立連接則拋出IO異常
*/
public Pipe() throws IOException {
pipedReader = new PipedReader();
pipedWriter = new PipedWriter();
pipedReader.connect(pipedWriter);
}
/**
* 從管道中讀一個(gè)字符
* @return 讀取到的一個(gè)字符
* @throws IOException 如不能從管道中讀則拋出異常
*/
public int read() throws IOException {
return pipedReader.read();
}
/**
* 向管道寫(xiě)一個(gè)字符
* @param c 要寫(xiě)入的字符
* @throws IOException 如不能向管道寫(xiě)則拋出異常
*/
public void write(int c) throws IOException {
pipedWriter.write(c);
}
//
// /**
// * 讀取一行數(shù)據(jù)
// * @return 讀取到的一行數(shù)據(jù)
// * @throws IOException 如果不能讀取到數(shù)據(jù)則拋出異常
// * 該方法只能讀到管道中的第一行數(shù)據(jù),不知道為什么?*****************
// */
// public String readLine() throws IOException {
// BufferedReader bufferedReader = new BufferedReader(pipedReader);
// return bufferedReader.readLine();
// }
//
/**
* 寫(xiě)入字符串的某一部分
* @param s 要寫(xiě)入的字符串
* @param off 開(kāi)始讀取字符處的偏移量
* @param len 要寫(xiě)入的總字符數(shù)
* @throws IOException 如不能寫(xiě)入數(shù)據(jù)則拋出異常
*/
public void write(String s, int off, int len) throws IOException {
pipedWriter.write(s.toCharArray(), off, len);
}
/**
* 寫(xiě)入整個(gè)字符串
* @param s 要寫(xiě)入的字符串
* @throws IOException 如不能寫(xiě)入數(shù)據(jù)則拋出異常
*/
public void write(String s) throws IOException {
pipedWriter.write(s.toCharArray());
}
/**
* 將讀管道關(guān)閉,調(diào)用該方法后,不能再?gòu)墓艿乐凶x數(shù)據(jù)
* @throws IOException 如不能關(guān)閉則拋出異常
*/
public void closeReader() throws IOException {
pipedReader.close();
}
/**
* 將寫(xiě)管道關(guān)閉,調(diào)用該方法后,不能向管道中寫(xiě)數(shù)據(jù)
* @throws IOException 如不能關(guān)閉則拋出異常
*/
public void closeWriter() throws IOException {
pipedWriter.flush();
pipedWriter.close();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -