1. 程式人生 > 實用技巧 >位元組輸出流OutputStream,位元組輸入流InputStream。。。字元輸入流Reader, 字元輸出流Writer

位元組輸出流OutputStream,位元組輸入流InputStream。。。字元輸入流Reader, 字元輸出流Writer

位元組流(byte)

位元組輸出流物件

位元組輸出流OutputStream

建立位元組流輸出物件,明確目的地
//構造方法,如果描述的檔案不存在則建立,存在則覆蓋
    FileOutputStream fos=new FileOutputStream("D:\\io0512\\byte.txt",true);
    //寫出一個位元組
       fos.write(100);
    //寫位元組陣列
    byte[] bytes={49,48,48};
    fos.write(bytes);
    fos.write(bytes, 1, 2);
    //寫漢字
    byte[] bb={-66
,-67,-68,-69}; fos.write("你好\r\n小小".getBytes());//換行 //釋放資源 fos.close();

處理異常

FileOutputStream fos=null;
    
    try {
        fos=new FileOutputStream("D:\\io0512\\byte.txt",true);
        fos.write(100);
        //寫位元組陣列
        byte[] bytes={49,48,48};
        fos.write(bytes);
        fos.write(bytes, 
1, 2); //寫漢字 byte[] bb={-66,-67,-68,-69}; fos.write("你好\r\n小小".getBytes());//換行 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(fos!=null){ try { fos.close(); }
catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

位元組輸入流InputStream

一個位元組一個位元組讀

public class Demo03 {//輸入流
public static void main(String[] args) throws IOException {
    //建立位元組輸入流物件,明確資料來源
    FileInputStream fis=new FileInputStream("D:\\io0512\\byte.txt");
    //一個位元組一個位元組讀
    int len=0;
    /*while(len!=-1){
    System.out.println((char)len);
    len=fis.read();
    }*/
    while((len=fis.read())!=-1){
        System.out.println((char)len);
        
        }
    fis.close();
}
}

陣列讀

public class Demo04 {
public static void main(String[] args) throws IOException {
    FileInputStream fis=new FileInputStream("D:\\io0512\\byte.txt");
    byte[] bytes=new byte[2];
    int len=0;
    
    while((len=fis.read(bytes))!=-1){
    
    System.out.println(new String(bytes,0,len));//從0開始,轉len個
    }
    fis.close();
}
}

複製檔案

原理;讀取一個已有的資料,並將這些讀到的資料寫入到另一個檔案中。

從一個資料夾向另一個資料夾複製,一個位元組一個位元組複製

public class Demo05 {
public static void main(String[] args) throws IOException {
    //
    FileInputStream fis=new FileInputStream("D:\\io0512\\byte.txt");
    
    FileOutputStream fos=new FileOutputStream("D:\\io0512\\a\\byte.txt");
    
    int len=0;
    while((len=fis.read())!=-1){
        fos.write(len);
    }
    fis.close();
    fos.close();
}
}

一個數組一個數組複製

public class Demo06 {
public static void main(String[] args) throws IOException {
    FileInputStream in=new FileInputStream("D:\\io0512\\byte.txt");
    FileOutputStream on=new FileOutputStream("D:\\io0512\\a\\byte2.txt");
    int len=0;
    byte[] bytes=new byte[2];
    while((len=in.read(bytes))!=-1){
        on.write(bytes,0,len);
    }
    in.close();
    on.close();
}
}

字元流(char)

字元編碼表

編碼表:其實就是生活中字元和計算機二進位制的對應關係表。

1、ascii: 一個位元組中的7位就可以表示。對應的位元組都是正數。0-xxxxxxx

2、iso-8859-1:拉丁碼錶 latin,用了一個位元組用的8位。1-xxxxxxx 負數。

3、GB2312:簡體中文碼錶。包含6000-7000中文和符號。用兩個位元組表示。兩個位元組第一個位元組是負數,第二個位元組可能是正數

GBK:目前最常用的中文碼錶,2萬的中文和符號。用兩個位元組表示,其中的一部分文字,第一個位元組開頭是1,第二位元組開頭是0

GB18030:最新的中文碼錶,目前還沒有正式使用。

1、unicode:國際標準碼錶:無論是什麼文字,都用兩個位元組儲存。

l Java中的char型別用的就是這個碼錶。char c = 'a';佔兩個位元組。

l Java中的字串是按照系統預設碼錶來解析的。簡體中文版 字串預設的碼錶是GBK。

5、UTF-8:基於unicode,一個位元組就可以儲存資料,不要用兩個位元組儲存,而且這個碼錶更加的標準化,在每一個位元組頭加入了編碼資訊(後期到api中查詢)。

能識別中文的碼錶:GBK、UTF-8;正因為識別中文碼錶不唯一,涉及到了編碼解碼問題。

對於我們開發而言;常見的編碼 GBK UTF-8 ISO-8859-1

文字--->(數字) :編碼。 “abc”.getBytes() byte[]

(數字)--->文字 : 解碼。 byte[] b={97,98,99} new String(b,0,len)

字元輸入流Reader

FileReader類

public class CharStreamDemo {
    public static void main(String[] args) throws IOException {
        //給檔案中寫中文
        writeCNText();
        //讀取檔案中的中文
        readCNText();
    }    
    //讀取中文
    public static void readCNText() throws IOException {
        FileReader fr = new FileReader("D:\\test\\cn.txt");
        int ch = 0;
        while((ch = fr.read())!=-1){
            //輸出的字元對應的編碼值
            System.out.println(ch);
            //輸出字元本身
            System.out.println((char)ch);
        }
    }
    //寫中文
    public static void writeCNText() throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\test\\cn.txt");
        fos.write("歡迎你".getBytes());
        fos.close();
    }
}

字元輸出流Writer

FileWriter類

FileWriter寫入中文到檔案中

public class FileWriterDemo {
    public static void main(String[] args) throws IOException {
        //演示FileWriter 用於操作檔案的便捷類。

FileWriter shuchu=new FileWriter("D:\\io0512\\write.txt");//加true不覆蓋,續寫
shuchu.write(100);
shuchu.flush();//字元流刷
char[] c={'你','中','a','0'};
shuchu.write(c);
shuchu.write(c,1,1);
shuchu.write("海綿寶寶");

shuru.write("你好謝謝再見");//這些文字都要先編碼。都寫入到了流的緩衝區中。

        shuru.flush();


shuchu.close();


        
    }
}

flush()和close()的區別?

flush():將流中的緩衝區緩衝的資料重新整理到目的地中,重新整理後,流還可以繼續使用。

close():關閉資源,但在關閉前會將緩衝區中的資料先重新整理到目的地,否則丟失資料,然後在關閉流。流不可以使用。如果寫入資料多,一定要一邊寫一邊重新整理,最後一次可以不重新整理,由close完成重新整理並關閉。

複製文字檔案

一個字元一個字元讀,寫

public class Demo04 {
public static void main(String[] args) throws IOException {
    FileReader shuru=new FileReader("D:\\io0512\\write.txt");
    FileWriter shuchu=new FileWriter("D:\\io0512\\a\\write.txt");
    int len=0;
    while((len=shuru.read())!=-1){
        shuchu.write(len);
        shuchu.flush();
    }
    shuru.close();
    shuchu.close();
}
}

一個數組一個數組讀,寫

public class Demo05 {
    //字元流,只能操作文字檔案
public static void main(String[] args) throws IOException {
    FileReader shuru=new FileReader("D:\\io0512\\write.txt");
    FileWriter shuchu=new FileWriter("D:\\io0512\\a\\write.txt");
    int len=0;
    char[] c=new char[2];
    while((len=shuru.read(c))!=-1){
        shuchu.write(new String(c,0,len) );
        shuchu.flush();
    }
    shuru.close();
    shuchu.close();
}
}