JAVA學習第五十七課 ― IO流(十一)
來源:程序員人生 發布時間:2014-11-05 08:24:10 閱讀次數:3308次
1、管道流
PipedInputStream 和 PipedOutPutStream
輸入和輸出可以直接進行連接,結合線程使用
管道流,顧名思義,寫1個讀1個,連成1個管子
API文檔:管道輸入流應當連接到管道輸出流;管道輸入流提供要寫入管道輸出流的所有數據字節。通常,數據由某個線程從
PipedInputStream
對象讀取,并由其他線程將其寫入到相應的 PipedOutputStream
。不建議對這兩個對象嘗試使用單個線程,由于這樣可能死鎖線程。管道輸入流包括1個緩沖區,可在緩沖區限定的范圍內將讀操作和寫操作分離開。如果向連接收道輸出流提供數據字節的線程不再存在,則認為該管道已破壞
class Input implements Runnable{
private PipedInputStream pis;
Input(PipedInputStream pis){
this.pis = pis;
}
@Override
public void run() {
try {
byte[] by = new byte[1024];
int len = pis.read(by);
String str = new String(by,0,len);
System.out.println("str = "+str);
pis.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
class Output implements Runnable{
private PipedOutputStream pos;
Output(PipedOutputStream pos) {
// TODO Auto-generated constructor stub
this.pos = pos;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(5000);
pos.write("你好".getBytes());//字符串轉字節流
} catch (Exception e) {
// TODO: handle exception
}
}
}
public class Main {
public static void main(String[] args)throws IOException{
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
pis.connect(pos);//兩流對接
Thread t1 = new Thread(new Input(pis));
Thread t2 = new Thread(new Output(pos));
t1.start();
t2.start();
}
}
管道流利用不是很多,但是其本身很有特點
IO包中的其他類
操作基本類型數據的流對象―DataInputStream與DataOutputStream
操作字節數組―ByteArrayInputStream與ByteArrayOutputStream
操作字符數組―CharArrayInputStream與CharArrayOutoutStream
操作字符串―StringWriter與StringReader
DataInputStream與DataOutputStream
public static void read() throws IOException {
// TODO Auto-generated method stub
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
String str = dis.readUTF();
System.out.println(str);
dis.close();
}
public static void DataStreamDemo() throws IOException {
// TODO Auto-generated method stub
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeUTF("你好胖");以與機器無關方式使用 <a target=_blank href="">UTF⑻ 修改版</a>編碼將1個字符串寫入基礎輸出流。
dos.close();
}
剩下的方法和OutputStream、InputStream差不多
操作數組的流
源和匯都是內存。ByteArrayInputStream、ByteArrayOutputStream
ByteArrayOutputStream:此類實現了1個輸出流,其中的數據被寫入1個
byte 數組。緩沖區會隨著數據的不斷寫入而自動增長。可以使用 toByteArray()
和 toString()
獲得數據,關閉
ByteArrayOutputStream 無效。
public static void ByteStreamDemo() throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream("asdfd".getBytes());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = 0;
while((len = bis.read())!=⑴){
bos.write(len);
}
System.out.println(bos.toString());
}
剩下的流用法和ByteArrayInputStream都是相同的
編碼問題
編碼問題不過就是文字和2進制之間的相互轉換
字符串->字符數組:編碼
字符數組->字符串:解碼
public static void Demo() {
String str = "你好";
//編碼
byte[] by = str.getBytes();
for(byte b : by){
System.out.println(b);
}
//解碼
String str1 = new String(by);// == new String(by,"GBK");
System.out.println(str1);
}
編碼編對了,有可能能成功解碼
練習:依照最長字節數截取字符串
在java中字符串“abcd”與“ab你好”長度1樣,都是4個字符,但是字節數不1樣
定義方法,依照最大的字節數來取字節
如:“ab你好”為例,如果依照3個字節來取文字,取“ab”和半個你,半個舍棄,4個字節取“ab你”,5個字節還是“ab你”
public static void main(String[] args)throws IOException{
String str = "ab你好asdas撒旦發射的";
int len = str.getBytes("GBK").length;
for(int i = 0;i<len;i++){//依照i個字節取
System.out.println("OK : "+Demo(str,i+1));
}
}
public static String Demo(String str,int len) throws IOException {
byte[] by = str.getBytes("GBK");
int count = 0;//記錄從哪一個索引開始,字節不是漢字
for(int i = len⑴;i>=0;i-- ){
if(by[i]<0){
count++;
}else {
break;
}
}
//漢字對應2個字節,漢字的個數 = 有負字節數/2
return (count & 1) == 0?new String(by,0,len):new String(by,0,len⑴);
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈