JAVA學習第五十七課 — IO流(十一),javaio

來源:互聯網
上載者:User

JAVA學習第五十七課 — IO流(十一),javaio

一、管道流

PipedInputStream 和 PipedOutPutStream

輸入和輸出可以直接進行串連,結合線程使用

管道流,顧名思義,寫一個讀一個,連成一個管子

API文檔:管道輸入資料流應該串連到管道輸出資料流;管道輸入資料流提供要寫入管道輸出資料流的所有資料位元組。通常,資料由某個線程從PipedInputStream 對象讀取,並由其他線程將其寫入到相應的 PipedOutputStream。不建議對這兩個對象嘗試使用單個線程,因為這樣可能死結線程。管道輸入資料流包含一個緩衝區,可在緩衝區限定的範圍內將讀操作和寫操作分離開。如果向串連管道輸出資料流提供資料位元組的線程不再存在,則認為該管道已損壞

class Input implements Runnable{private PipedInputStream pis;Input(PipedInputStream pis){this.pis = pis;}@Overridepublic 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 stubthis.pos = pos;}@Overridepublic void run() {// TODO Auto-generated method stubtry {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 stubDataInputStream 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 stubDataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));dos.writeUTF("你好胖");以與機器無關方式使用 <a target=_blank href="">UTF-8 修改版</a>編碼將一個字串寫入基礎輸出資料流。dos.close();}
剩下的方法和OutputStream、InputStream差不多


運算元組的流


源和匯都是記憶體。ByteArrayInputStream、ByteArrayOutputStream

ByteArrayOutputStream:此類實現了一個輸出資料流,其中的資料被寫入一個 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())!=-1){bos.write(len);}System.out.println(bos.toString());}

剩下的流用法和ByteArrayInputStream都是相同的

編碼問題

編碼問題無非就是文字和二進位之間的相互轉換

字串->字元數組:編碼

字元數組->字串:解碼

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你好”長度一樣,都是4個字元,但是位元組數不一樣定義方法,按照最大的位元組數來取位元組如:“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-1;i>=0;i-- ){if(by[i]<0){count++;}else {break;}}//漢字對應2個位元組,漢字的個數 = 有負位元組數/2return (count & 1) == 0?new String(by,0,len):new String(by,0,len-1);}





java的io流這章到底該怎學?很

有字元流,位元組流。
字元流一般是需要處理字串的時候用的,比如按行讀,對讀出來的字串進行替換,比較等。
位元組流一般就是簡單的讀或者寫檔案。
具體到每個細的,就需要經驗積累了,常用的就那幾個。
字元流就是BufferedWriter,BufferedReader.
位元組流就是FileInputStream,FileOutputStream,DataInputStream,DataOutputStream。

有問題繼續問。
 
java 問題 Io流問題 這個是我學習寫的小程式

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class TEST {
public static void print(Object obj){
System.out.println(obj);
}
public static void main(String[] args) throws Exception{
FileInputStream fip = new FileInputStream("D:\\Nokia Ovi Suite 許可協議.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fip));
FileOutputStream fos = new FileOutputStream("D:\\Nokia Ovi Suite 許可協議1.txt");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
String temp;
while((temp=br.readLine())!=null) {
System.out.println(temp);
bw.write(temp);
bw.newLine();
}
br.close();
bw.close();
fip.close();
fos.close();
}

}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.