1. 程式人生 > >Gorilla帶您學java之流之深入

Gorilla帶您學java之流之深入

轉換流

轉換流呢是可以查指定編碼表進行讀寫的流。 OutputStreamWriter(字元流轉向位元組流的橋樑) InputStreamReader(位元組流轉向字元流的橋樑) 具體操作如下:

    // 轉換流寫個UTF-8格式的檔案
    public static void getWriterUTF8() throws IOException {

        // 檔案位元組流
        FileOutputStream fos = new FileOutputStream("xx/Test/hong.txt");
        // 建立轉換流 一個引數的是使用操作平臺的預設編碼格式寫檔案
OutputStreamWriter osw = new OutputStreamWriter(fos); // 寫 osw.write("無恥老賊"); // 關閉資源 1.多個流巢狀 只需要關閉最外層的流 2.自己創建出來的流 自己關 3.從系統中獲取的流不用管 osw.close(); } // 轉換流寫個GBK格式的檔案 public static void getWriterGBK() throws IOException { // 檔案位元組流 FileOutputStream fos = new
FileOutputStream("xx/Desktop/Test/jie.txt"); // 建立轉換流 編碼格式 大小寫都行 OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK"); // 寫 osw.write("無恥老賊"); // 關閉資源 1.多個流巢狀 只需要關閉最外層的流 2.自己創建出來的流 自己關 3.從系統中獲取的流不用管 osw.close(); } // 讀UTF-8 public static void getReaderUTF8
() throws IOException{ FileInputStream fis = new FileInputStream("xx/Desktop/Test/hong.txt"); InputStreamReader isr = new InputStreamReader(fis); char[] c = new char[1024]; int len = 0; while ((len = isr.read(c)) != -1) { System.out.println(new String(c, 0, len)); } isr.close(); } // 讀GBK public static void getReaderGBK() throws IOException{ FileInputStream fis = new FileInputStream("xx/Desktop/Test/hong.txt"); InputStreamReader isr = new InputStreamReader(fis, "GBK"); char[] c = new char[1024]; int len = 0; while ((len = isr.read(c)) != -1) { System.out.println(new String(c, 0, len)); } isr.close(); }

緩衝流(高效流)

緩衝位元組流

即BufferedOutputStream,預設緩衝區大小: 8K 8192 具體操作:

        // 建立一個緩衝流
        FileOutputStream fos = new FileOutputStream("xx/Desktop/Test/liu.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        // 寫入
        bos.write("xx".getBytes());

        bos.close();

        // 緩衝流讀取
        FileInputStream fis = new FileInputStream("xx/Desktop/Test/liu.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);

        byte[] b = new byte[1024];
        int len = 0;
        while ((len = bis.read(b)) != -1) {
            System.out.println(new String(b, 0, len));
        }

        bis.close();

高效字元流

即BufferedWriter BufferedReader 具體操作:

        FileWriter fw = new FileWriter("xx/Desktop/Test/liu.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        // 寫
        bw.write("白日依山盡");
        bw.newLine();// 換行
        bw.write("黃河入海流");
        bw.newLine();// 換行
        bw.flush();
        bw.close();

        FileReader fReader = new FileReader("xx/Desktop/Test/liu.txt");
        BufferedReader bufferedReader = new BufferedReader(fReader);
        FileWriter fw2 = new FileWriter("xx/Desktop/Test/liu123.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(fw2);

        String str = "";
        // 讀取一行字串方法 讀到末尾 返回null
        while ((str = bufferedReader.readLine()) != null) {
            /*注意: 該方法不能讀取到換行符
            System.out.print(str);
            */
            bufferedWriter.write(str);
            // 注意:複製檔案時,需要加入換行
            // 複製檔案會多一個換行
            bufferedWriter.newLine();
            bufferedWriter.flush();
        }
        bufferedReader.close();
        bufferedWriter.close();

Properties集合

是一個雙列集合 是Hashtable的子類。 該集合是唯一一個能和IO流有關係的集合。 一般該集合只儲存 字串型別的鍵值對。 load 將檔案直接讀入到集合中。 store 將集合中的鍵值對直接寫入到檔案中。

讀取檔案的格式: key=value 1.注意等號前後別加空格 2.一般該檔案使用.properties為字尾(起標識作用) 3.#為註釋 具體操作:

        // 將檔案直接讀入集合中
        FileInputStream fis = new FileInputStream("xx/Desktop/Test/wangge.properties");
        // 建立集合
        Properties properties = new Properties();
        // 載入檔案
        properties.load(fis);

        // 迭代器遍歷
        Enumeration names = properties.propertyNames();
        while (names.hasMoreElements()) {
            String str = (String) names.nextElement();
            System.out.println(str + "=" + properties.getProperty(str));
        }

        fis.close();
        FileOutputStream fos = new FileOutputStream("xx/Desktop/Test/wl.properties");
        Properties properties = new Properties();
        // 儲存鍵值對
        properties.setProperty("name", "wangge");
        properties.setProperty("age", "30");
        properties.setProperty("gender", "nan");
        // 寫入到檔案中
        // 引數2 是傳入的註釋 推薦英文
        properties.store(fos, "這是一個fos流");
        fos.close();

序列化流

將物件直接寫入到檔案中即ObjectOutputStream、ObjectInputStream。 具體操作:

    // 需要建立一個物件類 然後才能對這個操作序列化流
    public static void readObject() throws FileNotFoundException, IOException, ClassNotFoundException {
        // 讀取檔案
        FileInputStream fis = new FileInputStream("xx/Desktop/Test/mj.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        // ClassNotFoundException 找不到這個類
        // 將檔案中物件 進行反序列化 需要依賴 存入物件的.class檔案
        Object object = ois.readObject();
        System.out.println(object);
        ois.close();
    }

    public static void writeObject() throws FileNotFoundException, IOException {
        // 將物件寫入檔案
        FileOutputStream fos = new FileOutputStream("xx/Desktop/Test/mj.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        // 寫入 NotSerializableException 要儲存的物件的類沒有實現序列化介面
        // Serializable 序列化介面是個標識行介面 標識這個類的物件可以進行序列化
        oos.writeObject(new Person("wangge", 30));
        // 關閉流
        oos.close();
    }