Java的簡單理解(21)---IO的字節流和字符流

Java

字節流: 可以處理一切文件,包括二進制,音頻,視頻,doc等等。
字符流: 只能處理純文本,全部為可見字符。.txt .html

1. 字節流

1. 文件的讀取
    public void Input() {
        // 1. 建立聯系 File對象
        File src = new File("E:/xp/test/a.txt");
        // 2. 選擇流
        InputStream is = null; // 提升作用域
        try {
            is = new FileInputStream(src);
            // 3. 操作 不斷讀取,緩沖數組
            byte[] car = new byte[1024];
            int len = 0; // 實際讀取的長度

            // 循環讀取,還回實際讀取的長度,空的話還回-1
            while ((len = is.read(car)) != -1) {
                // 輸出 字節數組轉成字符串
                String info = new String(car,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件不存在!");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件讀取失敗!");
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("文件流關閉失敗!");
                }
            }
        }
    }
2. 文件的寫出
    public void Output() {
        // 1. 建立聯系 File對象 目的地
        File dest = new File("E:/xp/test/a.txt");
        // 2. 選擇流,文件輸出流 OutputStream,FileOutputStream
        OutputStream os = null;
        try {
            // true,以追加方式寫出文件
            os = new FileOutputStream(dest,true);
            // 操作
            String str = "WM is a good man!";
            // 字符串轉字節數組
            byte[] data = str.getBytes();
            os.write(data,0,data.length);

            os.flush(); // 強制刷新出去
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件未找到!");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件寫出失敗!");
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("文件寫出失敗!");
                }
            }
        }
    }
3. 文件的拷貝
    /**
     * 文件的拷貝
     * @throws IOException
     */
    public void copyFile() throws IOException {

        // 1. 建立聯系 源文件+目的文件
        File src = new File("E:/xp/test/1.jpg");
        File dest = new File("E:/xp/test/2.jpg");

        // 2. 選擇流
        InputStream is = new FileInputStream(src);
        OutputStream os = new FileOutputStream(dest);

        byte[] car = new byte[1024];
        int len = 0;

        // 讀取
        while ((len = is.read(car)) != -1) {
            // 寫出
            os.write(car,0,len);
        }
        os.flush(); // 強制刷出
        os.close(); // 后出現的先關閉
        is.close();

    }

2. 字符流

1. 純文本讀取
public void reader() {
    // 創建源
    File file = new File("E:/xp/test/a.txt");
    // 選擇流
    Reader reader = null;

    try {
        reader = new FileReader(file);
        // 用于讀取時存放的
        char[] car = new char[1024];
        int len = 0;
        while ((len = (reader.read(car))) != -1) {
            // 字符數組轉換為字符串
            String string = new String(car,0,len);
            System.out.println(string);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.out.println("源文件不存在");
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
2. 純文本的寫入
public void write() {
    // 創建源
    File file = new File("E:/xp/test/b.txt");
    // 選擇流
    Writer writer = null;

    try {
        // true: 追加文件而不是覆蓋文件,默認是false
        writer = new FileWriter(file, true);
        // 寫出
        // \r\n: 換行
        String msg = "鋤禾日當午\r\n汗滴禾下土\r\n";
        writer.write(msg);
        writer.append("誰知盤中餐");

        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。