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();
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章