Java基礎:IO流之字節(jié)流和字符流

io流

1. 流的概念

流(stream)的概念源于UNIX中管道(pipe)的概念。在UNIX中,管道是一條不間斷的字節(jié)流,用來實現(xiàn)程序或進程間的通信,或讀寫外圍設備、外部文件等。

一個流,必有源端和目的端,它們可以是計算機內(nèi)存的某些區(qū)域,也可以是磁盤文件,甚至可以是Internet上的某個URL。

流的方向是重要的,根據(jù)流的方向,流可分為兩類:輸入流和輸出流。用戶可以從輸入流中讀取信息,但不能寫它。相反,對輸出流,只能往輸入流寫,而不能讀它。

實際上,流的源端和目的端可簡單地看成是字節(jié)的生產(chǎn)者和消費者,對輸入流,可不必關心它的源端是什么,只要簡單地從流中讀數(shù)據(jù),而對輸出流,也可不知道它的目的端,只是簡單地往流中寫數(shù)據(jù)。

形象的比喻——水流 ,文件======程序 ,文件和程序之間連接一個管道,水流就在之間形成了,自然也就出現(xiàn)了方向:可以流進,也可以流出.便于理解,這么定義流: 流就是一個管道里面有流水,這個管道連接了文件和程序。

2. IO流概述

大多數(shù)應用程序都需要實現(xiàn)與設備之間的數(shù)據(jù)傳輸,例如鍵盤可以輸入數(shù)據(jù),顯示器可以顯示程序的運行結(jié)果等。在Java中,將這種通過不同輸入輸出設備(鍵盤,內(nèi)存,顯示器,網(wǎng)絡等)之間的數(shù)據(jù)傳輸抽象的表述為“流”,程序允許通過流的方式與輸入輸出設備進行數(shù)據(jù)傳輸。Java中的“流”都位于Java.io包中,稱之為IO(輸入輸出)流。 IO流:即InputOutput的縮寫。

輸入流和輸出流相對于內(nèi)存設備而言。將外設中的數(shù)據(jù)讀取到內(nèi)存中:輸入。將內(nèi)存的數(shù)寫入到外設中:輸出。

IO流的特點:

  • IO流用來處理設備間的數(shù)據(jù)傳輸。
  • Java對數(shù)據(jù)的操作是通過流的方式。
  • Java用于操作流的對象都在IO包中。
  • 流按操作數(shù)據(jù)分為兩種:字節(jié)流和字符流。
  • 流按流向分為:輸入流和輸出流。

PS:流只能操作數(shù)據(jù),而不能操作文件。

3. 流的三種分類方式

  • 按流的方向分為:輸入流和輸出流
  • 按流的數(shù)據(jù)單位不同分為:字節(jié)流和字符流
  • 按流的功能不同分為:節(jié)點流和處理流
io

4. 流的層次結(jié)構(gòu)

IO流的常用基類:

  • 字節(jié)流的抽象基流:InputStream和OutputStream
  • 字符流的抽象基流:Reader和Writer

PS:此四個類派生出來的子類名稱都是以父類名作為子類名的后綴,以前綴為其功能;如InputStream子類FileInputStream;Reader子類FileReader

字符流的由來:
?
其實就是:字節(jié)流讀取文字字節(jié)數(shù)據(jù)后,不直接操作而是先查指定的編碼表,獲取對應的文字。再對這個文字進行操作。簡單說:字節(jié)流+編碼表。

4.1 InputStream

io流

4.2 OutputStream

io流

4.3 Reader

io流

4.4 Writer

io

5. 字節(jié)流

5.1 字節(jié)流寫數(shù)據(jù)

字節(jié)輸出流:抽象類OutputStream,實現(xiàn)類FileOutputStream

io流

字節(jié)輸出流操作步驟:

  • 創(chuàng)建字節(jié)輸出流對象
  • 寫數(shù)據(jù)
  • 釋放資源

字節(jié)流寫數(shù)據(jù)的方式

方法 說明
write(int b) 一次寫一個字節(jié)
write(byte[] b) 一次寫一個字節(jié)數(shù)組
write(byte[] b,int off,int len) 一次寫一個字節(jié)數(shù)組的一部分
flush() 刷新緩沖區(qū)
close() 釋放資源



如何實現(xiàn)數(shù)據(jù)的換行?

  • 為什么沒有換行呢?
    因為只是寫了字節(jié)數(shù)據(jù),并沒有寫入換行符號。
  • 如何實現(xiàn)呢?
    寫入換行符號即可

PS:不同的系統(tǒng)針對不同的換行符號識別是不一樣的?

  • windows:\r\n
  • linux:\n
  • Mac:\r
  • 而一些常見的個高級記事本,是可以識別任意換行符號的。

如何實現(xiàn)數(shù)據(jù)的追加寫入?

用構(gòu)造方法帶第二個參數(shù)是true的情況即可

package cn.itcast_01;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 需求:我要往一個文本文件中輸入一句話:"hello,io"
 * 
 * 分析:
 *  A:這個操作最好是采用字符流來做,但是呢,字符流是在字節(jié)流之后才出現(xiàn)的,所以,今天我先講解字節(jié)流如何操作。
 *  B:由于我是要往文件中寫一句話,所以我們要采用字節(jié)輸出流。
 */
public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        // 創(chuàng)建字節(jié)輸出流對象
        // FileOutputStream(File file)
        // File file = new File("fos.txt");
        // FileOutputStream fos = new FileOutputStream(file);
        // FileOutputStream(String name)
        FileOutputStream fos = new FileOutputStream("fos.txt");
        /*
         * 創(chuàng)建字節(jié)輸出流對象了做了幾件事情:
         * A:調(diào)用系統(tǒng)功能去創(chuàng)建文件
         * B:創(chuàng)建fos對象
         * C:把fos對象指向這個文件
         */
        
        //寫數(shù)據(jù)
        fos.write("hello,IO".getBytes());
        fos.write("java".getBytes());
        
        //釋放資源
        //關閉此文件輸出流并釋放與此流有關的所有系統(tǒng)資源。
        fos.close();
        /*
         * 為什么一定要close()呢?
         * A:讓流對象變成垃圾,這樣就可以被垃圾回收器回收了
         * B:通知系統(tǒng)去釋放跟該文件相關的資源
         */
        //java.io.IOException: Stream Closed
        //fos.write("java".getBytes());
    }
}

5.2 字節(jié)流寫數(shù)據(jù)的方式

package cn.itcast_01;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 字節(jié)輸出流操作步驟:
 * A:創(chuàng)建字節(jié)輸出流對象
 * B:調(diào)用write()方法
 * C:釋放資源
 * 
 * public void write(int b):寫一個字節(jié)
 * public void write(byte[] b):寫一個字節(jié)數(shù)組
 * public void write(byte[] b,int off,int len):寫一個字節(jié)數(shù)組的一部分
 */
public class FileOutputStreamDemo2 {
    public static void main(String[] args) throws IOException {
        // 創(chuàng)建字節(jié)輸出流對象
        // OutputStream os = new FileOutputStream("fos2.txt"); // 多態(tài)
        FileOutputStream fos = new FileOutputStream("fos2.txt");

        // 調(diào)用write()方法
        //fos.write(97); //97 -- 底層二進制數(shù)據(jù)    -- 通過記事本打開 -- 找97對應的字符值 -- a
        // fos.write(57);
        // fos.write(55);
        
        //public void write(byte[] b):寫一個字節(jié)數(shù)組
        byte[] bys={97,98,99,100,101};
        fos.write(bys);
        
        //public void write(byte[] b,int off,int len):寫一個字節(jié)數(shù)組的一部分
        fos.write(bys,1,3);
        
        //釋放資源
        fos.close();
    }
}

字節(jié)流寫數(shù)據(jù)加入異常處理

package cn.itcast_01;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 加入異常處理的字節(jié)輸出流操作
 */
public class FileOutputStreamDemo4 {
    public static void main(String[] args) {
        // 分開做異常處理
        // FileOutputStream fos = null;
        // try {
        // fos = new FileOutputStream("fos4.txt");
        // } catch (FileNotFoundException e) {
        // e.printStackTrace();
        // }
        //
        // try {
        // fos.write("java".getBytes());
        // } catch (IOException e) {
        // e.printStackTrace();
        // }
        //
        // try {
        // fos.close();
        // } catch (IOException e) {
        // e.printStackTrace();
        // }

        // 一起做異常處理
        // try {
        // FileOutputStream fos = new FileOutputStream("fos4.txt");
        // fos.write("java".getBytes());
        // fos.close();
        // } catch (FileNotFoundException e) {
        // e.printStackTrace();
        // } catch (IOException e) {
        // e.printStackTrace();
        // }

        // 改進版
        // 為了在finally里面能夠看到該對象就必須定義到外面,為了訪問不出問題,還必須給初始化值
        FileOutputStream fos = null;
        try {
            // fos = new FileOutputStream("z:\\fos4.txt");
            fos = new FileOutputStream("fos4.txt");
            fos.write("java".getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 如果fos不是null,才需要close()
            if (fos != null) {
                // 為了保證close()一定會執(zhí)行,就放到這里了
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

5.3 字節(jié)流讀取數(shù)據(jù)

字節(jié)輸入流:抽象類InputStream,實現(xiàn)類FileInputStream

io流

字節(jié)輸入流操作步驟:

  • A:創(chuàng)建字節(jié)輸入流對象
  • B:調(diào)用read()方法讀取數(shù)據(jù),并把數(shù)據(jù)顯示在控制臺
  • C:釋放資源

字節(jié)流讀數(shù)據(jù)的方式

方法 功能描述
int read() 一次讀取一個字節(jié)
int read(byte[] b) 一次讀取一個字節(jié)數(shù)組
int read(byte[] b,int off,int len) 一次讀一個字節(jié)數(shù)組的一部分
void close() 釋放資源


package cn.itcast_02;
import java.io.FileInputStream;
import java.io.IOException;
/*
 * 字節(jié)輸入流操作步驟:
 * A:創(chuàng)建字節(jié)輸入流對象
 * B:調(diào)用read()方法讀取數(shù)據(jù),并把數(shù)據(jù)顯示在控制臺
 * C:釋放資源
 * 
 * 讀取數(shù)據(jù)的方式:
 * A:int read():一次讀取一個字節(jié)
 * B:int read(byte[] b):一次讀取一個字節(jié)數(shù)組
 * C:int read(byte[] b):一次讀取一個字節(jié)數(shù)組
 *   返回值其實是實際讀取的字節(jié)個數(shù)。
 */
public class FileInputStreamDemo {
    public static void main(String[] args) throws IOException {
        // FileInputStream(String name)
        // FileInputStream fis = new FileInputStream("fis.txt");
        FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");

        // // 調(diào)用read()方法讀取數(shù)據(jù),并把數(shù)據(jù)顯示在控制臺
        // // 第一次讀取
        // int by = fis.read();
        // System.out.println(by);
        // System.out.println((char) by);
        //
        // // 第二次讀取
        // by = fis.read();
        // System.out.println(by);
        // System.out.println((char) by);
        //
        // // 第三次讀取
        // by = fis.read();
        // System.out.println(by);
        // System.out.println((char) by);
        // // 我們發(fā)現(xiàn)代碼的重復度很高,所以我們要用循環(huán)改進
        // // 而用循環(huán),最麻煩的事情是如何控制循環(huán)判斷條件呢?
        // // 第四次讀取
        // by = fis.read();
        // System.out.println(by);
        // // 第五次讀取
        // by = fis.read();
        // System.out.println(by);
        // //通過測試,我們知道如果你讀取的數(shù)據(jù)是-1,就說明已經(jīng)讀取到文件的末尾了

        // 用循環(huán)改進
        // int by = fis.read();
        // while (by != -1) {
        // System.out.print((char) by);
        // by = fis.read();
        // }

        // 最終版代碼
        int by = 0;
        // 讀取,賦值,判斷
        while ((by = fis.read()) != -1) {
            System.out.print((char) by);
        }
                // 數(shù)組的長度一般是1024或者1024的整數(shù)倍
        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = fis.read(bys)) != -1) {
            System.out.print(new String(bys, 0, len));
        }

        // 釋放資源
        fis.close();
    }
}

5.4 字節(jié)流復制數(shù)據(jù)練習

代碼示例:把c:\a.txt內(nèi)容復制到d:\b.txt中

package cn.itcast_03;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 需求:把c盤下的a.txt的內(nèi)容復制到d盤下的b.txt中
 * 
 * 數(shù)據(jù)源:
 *      c:\\a.txt   --  讀取數(shù)據(jù)--  FileInputStream
 * 目的地:
 *      d:\\b.txt   --  寫出數(shù)據(jù)    --  FileOutputStream
 */
public class CopyFileDemo2 {
    public static void main(String[] args) throws IOException {
        // 封裝數(shù)據(jù)源
        FileInputStream fis = new FileInputStream("c:\\a.txt");
        // 封裝目的地
        FileOutputStream fos = new FileOutputStream("d:\\b.txt");

        // 復制數(shù)據(jù)
        int by = 0;
        while ((by = fis.read()) != -1) {
            fos.write(by);
        }

        // 釋放資源
        fos.close();
        fis.close();
    }
}

4、字節(jié)緩沖流

字節(jié)流一次讀寫一個數(shù)組的速度明顯比一次讀寫一個字節(jié)的速度快很多,這是加入了數(shù)組這樣的緩沖區(qū)效果,java本身在設計的時候,也考慮到了這樣的設計思想(裝飾設計模式后面講解),所以提供了字節(jié)緩沖區(qū)流。

字節(jié)緩沖輸出流:BufferedOutputStream,字節(jié)緩沖輸入流:BufferedInputStream。

package cn.itcast_05;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 通過定義數(shù)組的方式確實比以前一次讀取一個字節(jié)的方式快很多,所以,看來有一個緩沖區(qū)還是非常好的。
 * 既然是這樣的話,那么,java開始在設計的時候,它也考慮到了這個問題,就專門提供了帶緩沖區(qū)的字節(jié)類。
 * 這種類被稱為:緩沖區(qū)類(高效類)
 * 寫數(shù)據(jù):BufferedOutputStream
 * 讀數(shù)據(jù):BufferedInputStream
 * 
 * 構(gòu)造方法可以指定緩沖區(qū)的大小,但是我們一般用不上,因為默認緩沖區(qū)大小就足夠了。
 * 
 * 為什么不傳遞一個具體的文件或者文件路徑,而是傳遞一個OutputStream對象呢?
 * 原因很簡單,字節(jié)緩沖區(qū)流僅僅提供緩沖區(qū),為高效而設計的。但是呢,真正的讀寫操作還得靠基本的流對象實現(xiàn)。
 */
public class BufferedOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        // BufferedOutputStream(OutputStream out)
        // FileOutputStream fos = new FileOutputStream("bos.txt");
        // BufferedOutputStream bos = new BufferedOutputStream(fos);
        // 簡單寫法
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("bos.txt"));

        // 寫數(shù)據(jù)
        bos.write("hello".getBytes());

        // 釋放資源
        bos.close();
    }
}

5.5 字節(jié)緩沖流復制數(shù)據(jù)練習

package cn.itcast_06;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 需求:把e:\\a.mp4復制到當前項目目錄下的b.mp4中
 * 
 * 字節(jié)流四種方式復制文件:
 * 基本字節(jié)流一次讀寫一個字節(jié):   共耗時:117235毫秒
 * 基本字節(jié)流一次讀寫一個字節(jié)數(shù)組: 共耗時:156毫秒
 * 高效字節(jié)流一次讀寫一個字節(jié): 共耗時:1141毫秒
 * 高效字節(jié)流一次讀寫一個字節(jié)數(shù)組: 共耗時:47毫秒
 */
public class CopyMp4Demo {
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        // method1("e:\\a.mp4", "copy1.mp4");
        // method2("e:\\a.mp4", "copy2.mp4");
        // method3("e:\\a.mp4", "copy3.mp4");
        method4("e:\\a.mp4", "copy4.mp4");
        long end = System.currentTimeMillis();
        System.out.println("共耗時:" + (end - start) + "毫秒");
    }

    // 高效字節(jié)流一次讀寫一個字節(jié)數(shù)組:
    public static void method4(String srcString, String destString)
            throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                srcString));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(destString));

        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = bis.read(bys)) != -1) {
            bos.write(bys, 0, len);
        }

        bos.close();
        bis.close();
    }

    // 高效字節(jié)流一次讀寫一個字節(jié):
    public static void method3(String srcString, String destString)
            throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                srcString));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(destString));

        int by = 0;
        while ((by = bis.read()) != -1) {
            bos.write(by);

        }

        bos.close();
        bis.close();
    }

    // 基本字節(jié)流一次讀寫一個字節(jié)數(shù)組
    public static void method2(String srcString, String destString)
            throws IOException {
        FileInputStream fis = new FileInputStream(srcString);
        FileOutputStream fos = new FileOutputStream(destString);

        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = fis.read(bys)) != -1) {
            fos.write(bys, 0, len);
        }

        fos.close();
        fis.close();
    }

    // 基本字節(jié)流一次讀寫一個字節(jié)
    public static void method1(String srcString, String destString)
            throws IOException {
        FileInputStream fis = new FileInputStream(srcString);
        FileOutputStream fos = new FileOutputStream(destString);

        int by = 0;
        while ((by = fis.read()) != -1) {
            fos.write(by);
        }

        fos.close();
        fis.close();
    }
}

6. 字符流

6.1 轉(zhuǎn)換流出現(xiàn)的原因及思想

由于字節(jié)流操作中文不是特別方便,所以,java就提供了轉(zhuǎn)換流。字符流=字節(jié)流+編碼表。

編碼表:由字符及其對應的數(shù)值組成的一張表

常見編碼表

計算機只能識別二進制數(shù)據(jù),早期由來是電信號。為了方便應用計算機,讓它可以識別各個國家的文字。就將各個國家的文字用數(shù)字來表示,并一一對應,形成一張表。

io流

字符串中的編碼問題

編碼:把看得懂的變成看不懂的
解碼:把看不懂的變成看得懂

package cn.itcast_01;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/*
 * String(byte[] bytes, String charsetName):通過指定的字符集解碼字節(jié)數(shù)組
 * byte[] getBytes(String charsetName):使用指定的字符集合把字符串編碼為字節(jié)數(shù)組
 * 
 * 編碼:把看得懂的變成看不懂的
 * String -- byte[]
 * 
 * 解碼:把看不懂的變成看得懂的
 * byte[] -- String
 * 
 * 舉例:諜戰(zhàn)片(發(fā)電報,接電報)
 * 
 * 碼表:小本子
 *      字符  數(shù)值
 * 
 * 要發(fā)送一段文字:
 *      今天晚上在老地方見
 * 
 *      發(fā)送端:今 -- 數(shù)值 -- 二進制 -- 發(fā)出去
 *      接收端:接收 -- 二進制 -- 十進制 -- 數(shù)值 -- 字符 -- 今
 * 
 *      今天晚上在老地方見
 * 
 * 編碼問題簡單,只要編碼解碼的格式是一致的。
 */
public class StringDemo {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String s = "你好";

        // String -- byte[]
        byte[] bys = s.getBytes(); // [-60, -29, -70, -61]
        // byte[] bys = s.getBytes("GBK");// [-60, -29, -70, -61]
        // byte[] bys = s.getBytes("UTF-8");// [-28, -67, -96, -27, -91, -67]
        System.out.println(Arrays.toString(bys));

        // byte[] -- String
        String ss = new String(bys); // 你好
        // String ss = new String(bys, "GBK"); // 你好
        // String ss = new String(bys, "UTF-8"); // ???
        System.out.println(ss);
    }
}

6.2 轉(zhuǎn)換流概述

OutputStreamWriter 字符輸出流

io流

InputStreamReader 字符輸入流

io流

OutputStreamWriter寫數(shù)據(jù)

方法 功能描述
write(int c) 寫入一個字符
write(char[] cbuf) 寫入一個字符數(shù)組
write(char[] cbuf,int off,int len) 寫入一個字符數(shù)組的一部分
write(String str) 寫入一個字符串
write(String str,int off,int len) 寫入一個字符串的一部分



字符流操作要注意的問題:

字符流數(shù)據(jù)沒有直接進文件而是到緩沖區(qū),所以要刷新緩沖區(qū)。

flush()和close()的區(qū)別:

  • A:close()關閉流對象,但是先刷新一次緩沖區(qū)。關閉之后,流對象不可以繼續(xù)再使用了。
  • B:flush()僅僅刷新緩沖區(qū),刷新之后,流對象還可以繼續(xù)使用。
package cn.itcast_03;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/*
 * OutputStreamWriter的方法:
 * public void write(int c):寫一個字符
 * public void write(char[] cbuf):寫一個字符數(shù)組
 * public void write(char[] cbuf,int off,int len):寫一個字符數(shù)組的一部分
 * public void write(String str):寫一個字符串
 * public void write(String str,int off,int len):寫一個字符串的一部分
 */
public class OutputStreamWriterDemo {
    public static void main(String[] args) throws IOException {
        // 創(chuàng)建對象
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
                "osw2.txt"));

        // 寫數(shù)據(jù)
        // public void write(int c):寫一個字符
        // osw.write('a');
        // osw.write(97);
        // 為什么數(shù)據(jù)沒有進去呢?
        // 原因是:字符 = 2字節(jié)
        // 文件中數(shù)據(jù)存儲的基本單位是字節(jié)。
        // void flush()

        // public void write(char[] cbuf):寫一個字符數(shù)組
        // char[] chs = {'a','b','c','d','e'};
        // osw.write(chs);

        // public void write(char[] cbuf,int off,int len):寫一個字符數(shù)組的一部分
        // osw.write(chs,1,3);

        // public void write(String str):寫一個字符串
        // osw.write("我愛林青霞");

        // public void write(String str,int off,int len):寫一個字符串的一部分
        osw.write("我愛林青霞", 2, 3);

        // 刷新緩沖區(qū)
        osw.flush();
        // osw.write("我愛林青霞", 2, 3);

        // 釋放資源
        osw.close();
        // java.io.IOException: Stream closed
        // osw.write("我愛林青霞", 2, 3);
    }
}

6.3 InputStreamReader讀數(shù)據(jù)

  • public int read():一次讀一個字符
  • public int read(char[] cbuf):一次讀一個字符數(shù)組
package cn.itcast_03;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/*
 * InputStreamReader的方法:
 * int read():一次讀取一個字符
 * int read(char[] chs):一次讀取一個字符數(shù)組
 */
public class InputStreamReaderDemo {
    public static void main(String[] args) throws IOException {
        // 創(chuàng)建對象
        InputStreamReader isr = new InputStreamReader(new FileInputStream(
                "StringDemo.java"));

        // 一次讀取一個字符
        // int ch = 0;
        // while ((ch = isr.read()) != -1) {
        // System.out.print((char) ch);
        // }

        // 一次讀取一個字符數(shù)組
        char[] chs = new char[1024];
        int len = 0;
        while ((len = isr.read(chs)) != -1) {
            System.out.print(new String(chs, 0, len));
        }

        // 釋放資源
        isr.close();
    }
}

6.4 字符流復制文本文件

package cn.itcast_04;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/*
 * 需求:把當前項目目錄下的a.txt內(nèi)容復制到當前項目目錄下的b.txt中
 * 
 * 數(shù)據(jù)源:
 *      a.txt -- 讀取數(shù)據(jù) -- 字符轉(zhuǎn)換流 -- InputStreamReader
 * 目的地:
 *      b.txt -- 寫出數(shù)據(jù) -- 字符轉(zhuǎn)換流 -- OutputStreamWriter
 */
public class CopyFileDemo {
    public static void main(String[] args) throws IOException {
        // 封裝數(shù)據(jù)源
        InputStreamReader isr = new InputStreamReader(new FileInputStream(
                "a.txt"));
        // 封裝目的地
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
                "b.txt"));

        // 讀寫數(shù)據(jù)
        // 方式1
        // int ch = 0;
        // while ((ch = isr.read()) != -1) {
        // osw.write(ch);
        // }

        // 方式2
        char[] chs = new char[1024];
        int len = 0;
        while ((len = isr.read(chs)) != -1) {
            osw.write(chs, 0, len);
            // osw.flush();
        }

        // 釋放資源
        osw.close();
        isr.close();
    }
}

6.5 轉(zhuǎn)換流的簡化寫法

轉(zhuǎn)換流的名字比較長,而我們常見的操作都是按照本地默認編碼實現(xiàn)的,所以,為了簡化我們的書寫,轉(zhuǎn)換流提供了對應的子類。

FileWriter

io流

代碼示例:把當前項目目錄下的a.txt內(nèi)容復制到當前項目目錄下的b.txt中

package cn.itcast_04;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
 * 由于我們常見的操作都是使用本地默認編碼,所以,不用指定編碼。
 * 而轉(zhuǎn)換流的名稱有點長,所以,Java就提供了其子類供我們使用。
 * OutputStreamWriter = FileOutputStream + 編碼表(GBK)
 * FileWriter = FileOutputStream + 編碼表(GBK)
 * 
 * InputStreamReader = FileInputStream + 編碼表(GBK)
 * FileReader = FileInputStream + 編碼表(GBK)
 * 
 /*
 * 需求:把當前項目目錄下的a.txt內(nèi)容復制到當前項目目錄下的b.txt中
 * 
 * 數(shù)據(jù)源:
 *      a.txt -- 讀取數(shù)據(jù) -- 字符轉(zhuǎn)換流 -- InputStreamReader -- FileReader
 * 目的地:
 *      b.txt -- 寫出數(shù)據(jù) -- 字符轉(zhuǎn)換流 -- OutputStreamWriter -- FileWriter
 */
public class CopyFileDemo2 {
    public static void main(String[] args) throws IOException {
        // 封裝數(shù)據(jù)源
        FileReader fr = new FileReader("a.txt");
        // 封裝目的地
        FileWriter fw = new FileWriter("b.txt");

        // 一次一個字符
        // int ch = 0;
        // while ((ch = fr.read()) != -1) {
        // fw.write(ch);
        // }

        // 一次一個字符數(shù)組
        char[] chs = new char[1024];
        int len = 0;
        while ((len = fr.read(chs)) != -1) {
            fw.write(chs, 0, len);
            fw.flush();
        }

        // 釋放資源
        fw.close();
        fr.close();
    }
}

6.6 FileReader

io流

代碼示例:把c:\a.txt內(nèi)容復制到d:\b.txt中

package cn.itcast_04;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
 * 需求:把c:\\a.txt內(nèi)容復制到d:\\b.txt中
 * 
 * 數(shù)據(jù)源:
 *      c:\\a.txt -- FileReader
 * 目的地:
 *      d:\\b.txt -- FileWriter
 */
public class CopyFileDemo3 {
    public static void main(String[] args) throws IOException {
        // 封裝數(shù)據(jù)源
        FileReader fr = new FileReader("c:\\a.txt");
        // 封裝目的地
        FileWriter fw = new FileWriter("d:\\b.txt");

        // 讀寫數(shù)據(jù)
        // int ch = 0;
        int ch;
        while ((ch = fr.read()) != -1) {
            fw.write(ch);
        }
        
        //釋放資源
        fw.close();
        fr.close();
    }
}

7. 字符緩沖流

字符流為了高效讀寫,也提供了對應的字符緩沖流。BufferedWriter:字符緩沖輸出流,BufferedReader:字符緩沖輸入流。

7.1 BufferedWriter基本用法

將文本寫入字符輸出流,緩沖各個字符,從而提供單個字符、數(shù)組和字符串的高效寫入。 可以指定緩沖區(qū)的大小,或者接受默認的大小。在大多數(shù)情況下,默認值就足夠大了。

io流
io流

代碼示例:BufferedWriter基本用法

package cn.itcast_05;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
 * 字符流為了高效讀寫,也提供了對應的字符緩沖流。
 * BufferedWriter:字符緩沖輸出流
 * BufferedReader:字符緩沖輸入流
 * 
 * BufferedWriter:字符緩沖輸出流
 * 將文本寫入字符輸出流,緩沖各個字符,從而提供單個字符、數(shù)組和字符串的高效寫入。 
 * 可以指定緩沖區(qū)的大小,或者接受默認的大小。在大多數(shù)情況下,默認值就足夠大了。 
 */
public class BufferedWriterDemo {
    public static void main(String[] args) throws IOException {
        // BufferedWriter(Writer out)
        // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
        // new FileOutputStream("bw.txt")));

        BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));

        bw.write("hello");
        bw.write("world");
        bw.write("java");
        bw.flush();

        bw.close();
    }
}

7.2 BufferedReader基本用法

從字符輸入流中讀取文本,緩沖各個字符,從而實現(xiàn)字符、數(shù)組和行的高效讀取。 可以指定緩沖區(qū)的大小,或者可使用默認的大小。大多數(shù)情況下,默認值就足夠大了。

io流
io流

代碼示例: BufferedReader基本用法

package cn.itcast_05;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
 * BufferedReader
 * 從字符輸入流中讀取文本,緩沖各個字符,從而實現(xiàn)字符、數(shù)組和行的高效讀取。 
 * 可以指定緩沖區(qū)的大小,或者可使用默認的大小。大多數(shù)情況下,默認值就足夠大了。 
 * 
 * BufferedReader(Reader in)
 */
public class BufferedReaderDemo {
    public static void main(String[] args) throws IOException {
        // 創(chuàng)建字符緩沖輸入流對象
        BufferedReader br = new BufferedReader(new FileReader("bw.txt"));

        // 方式1
        // int ch = 0;
        // while ((ch = br.read()) != -1) {
        // System.out.print((char) ch);
        // }

        // 方式2
        char[] chs = new char[1024];
        int len = 0;
        while ((len = br.read(chs)) != -1) {
            System.out.print(new String(chs, 0, len));
        }

        // 釋放資源
        br.close();
    }
}

7.3 特殊功能

  • BufferedWriter,newLine():根據(jù)系統(tǒng)來決定換行符
  • BufferedReader,String readLine():一次讀取一行數(shù)據(jù)

代碼示例:字符緩沖流的特殊方法

package cn.itcast_05;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
 * 字符緩沖流的特殊方法:
 * BufferedWriter:
 *      public void newLine():根據(jù)系統(tǒng)來決定換行符
 * BufferedReader:
 *      public String readLine():一次讀取一行數(shù)據(jù)
 *      包含該行內(nèi)容的字符串,不包含任何行終止符,如果已到達流末尾,則返回 null
 */
public class BufferedDemo {
    public static void main(String[] args) throws IOException {
        // write();
        read();
    }

    private static void read() throws IOException {
        // 創(chuàng)建字符緩沖輸入流對象
        BufferedReader br = new BufferedReader(new FileReader("bw2.txt"));

        // public String readLine():一次讀取一行數(shù)據(jù)
        // String line = br.readLine();
        // System.out.println(line);
        // line = br.readLine();
        // System.out.println(line);

        // 最終版代碼
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        
        //釋放資源
        br.close();
    }

    private static void write() throws IOException {
        // 創(chuàng)建字符緩沖輸出流對象
        BufferedWriter bw = new BufferedWriter(new FileWriter("bw2.txt"));
        for (int x = 0; x < 10; x++) {
            bw.write("hello" + x);
            // bw.write("\r\n");
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }

}

代碼示例:字符緩沖流復制文本文件

package cn.itcast_06;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
 * 需求:把當前項目目錄下的a.txt內(nèi)容復制到當前項目目錄下的b.txt中
 * 
 * 數(shù)據(jù)源:
 *      a.txt -- 讀取數(shù)據(jù) -- 字符轉(zhuǎn)換流 -- InputStreamReader -- FileReader -- BufferedReader
 * 目的地:
 *      b.txt -- 寫出數(shù)據(jù) -- 字符轉(zhuǎn)換流 -- OutputStreamWriter -- FileWriter -- BufferedWriter
 */
public class CopyFileDemo2 {
    public static void main(String[] args) throws IOException {
        // 封裝數(shù)據(jù)源
        BufferedReader br = new BufferedReader(new FileReader("a.txt"));
        // 封裝目的地
        BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));

        // 讀寫數(shù)據(jù)
        String line = null;
        while ((line = br.readLine()) != null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        // 釋放資源
        bw.close();
        br.close();
    }
}

8. 總結(jié)

io流
io流
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容