1. 程式人生 > >Java之位元組流和字元流

Java之位元組流和字元流

一.位元組流

流分為輸入流和輸出流。 用參照物(程式)來判斷是輸出流還是輸入流。 注意:在用流的時候,結束時要記得關掉流。

位元組流 將文字、圖片、音訊等轉成位元組,進行資料傳輸。

  • 程式——>檔案 輸出流 寫檔案
  • 檔案——>程式 輸入流 讀檔案
  • 以下所有位元組流的父類, 是抽象類
    • OutputStream: 輸出流
    • InputStream : 輸入流

位元組流輸出流(寫入檔案的方法)程式碼:

public class Kll {
    public static void main(String[] args) {
        // 建立一個位元組輸出流 寫檔案
// 建立流 繫結寫入檔案路徑 // 寫入時,[路徑正確情況下]系統會幫你創建出檔案(沒有此檔案的情況下),否則會覆蓋原檔案 File file = new File("/Users/lanou/Desktop/Test/kll.txt"); FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/kll1.txt"); // 寫入方法 // 該方法是按ASCII碼寫入檔案 fos.write(48); fos.write(49
); fos.write(66); // 利用位元組陣列輸入 byte[] b = {66, 67, 68, 69}; fos.write(b); // "konglinglei"轉成位元組陣列 getBytes() fos.write("konglinglei".getBytes()); // 按偏移量寫入陣列字元 fos.write(b, 1, 2); // 關閉資源 fos.close(); } }

位元組流輸入流(讀取檔案的方法)程式碼:

1.一個一個讀取

public class Kll {
    public static void main(String[] args) {
        // 讀取
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/kll1.txt");
        // 一個一個位元組讀取,按照ASCII碼讀取
        int read = fis.read();
        System.out.println((char)read);
         read = fis.read();
        System.out.println((char)read);
         read = fis.read();
        System.out.println((char)read);
        // 檔案讀取完畢會返回-1
         read = fis.read();
        System.out.println(read);
        fis.close();

        // 迴圈快捷讀取方法
        /*
        int num = 0;
        while ((num = fis.read()) != -1) {
            System.out.println((char)num);
        }
        fis.close();
        */
    }
}

2.陣列讀取

public class Kll {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/kll1.txt");
        // 利用位元組陣列讀
        // 實際上把讀取的內容放回陣列中了
        // read(byte[] b) 方法返回值是讀取到的有效數字,讀完之後,返回的也是-1.
        // byte陣列長度,一般填1024或者1024整數倍
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = fis.read(b)) != -1) {
            // 使用字元長的構造方法列印  String(byte[] b, offset,length)
            String string = new String(b, 0, len);
            System.out.println(string);
            System.out.println(len);
        }
        fis.close();
    }
}

二.字元陣列流

與位元組流方法大同小異。

  • 字元流(讀寫文件)
  • 所有字元流的父類, 是抽象類
    • Writer
    • Reader
  • FileWriter
  • FileReader
  • 注意:在未關閉資源時,需要重新整理才能把寫入的文字放入文字中。具體看下列程式碼解釋。

1.字元輸出流程式碼:

public class Kll {
    public static void main(String[] args) {
        // 建立字元輸出流
        FileWriter fw = new FileWriter("/Users/lanou/Desktop/Test11/wl.txt");
        // 寫入檔案
        fw.write(65);
        // 重新整理 之後,會將寫入的內容寫入在文字中.
        //fw.flush();
        // 字元陣列寫入
        char[] c = {'了', '5', '8', '9'};
        fw.write(c);
        // 字串直接寫入
        // Mac 中 換行符 \n win /r /n
        fw.write("\n離離原上草\n一歲一枯榮\n野火燒不盡\n春風吹又生");

        // 資源關閉之前,系統會幫你重新整理一下
        fw.close();
    }
}

2.字元輸入流:

public class Kll {
    public static void main(String[] args) {
    // 建立字元輸入流
        FileReader fr = new FileReader("/Users/lanou/Desktop/Test11/wl.txt");
        // 一個位元組一個位元組讀
        /*int read = 0;
        while ((read = fr.read()) != -1) {
            System.out.print((char)read);
        }
        fr.close();
        */
        // 用陣列讀取
        char[] cbuf = new char[1024];
        int num = 0;
        while ((num = fr.read(cbuf)) != -1) {
            String string = new String(cbuf, 0, num);
            System.out.println(string);
            System.out.println(num);
        }
        fr.close();
    }
}