14 IO流

IO流

  1. IO流分类:
    如果是按照数据的流向划分:

    输入流
    输出流
    

    如果按照处理的单位划分:

    字节流: 字节流读取得都是文件中二进制数据,读取到二进制数据不会经过任何的处理
    字符流: 字符流读取的数据是以字符为单位的 。 字符流也是读取文件中的二进制数据,不过会把这些二进制数据转换成我们能 识别的字符。
    字符流 = 字节流 + 解码


FileInputStream

  1. 输入字节流:
    —–| InputStream 所有输入字节流的基类 抽象类
    ———| FileInputStream 读取文件数据的输入字节流

  2. 使用FileInputStream读取文件数据的步骤:

    1. 找到目标文件
    2. 建立数据的输入通道。
    3. 读取文件中的数据。
    4. 关闭资源.

读取文件四种方式

//案例中比较2、4两种方法的速度
//方式4:使用缓冲数组配合循环一起读取
public static void readTest4() throws IOException{
        long startTime = System.currentTimeMillis();
        //找到目标文件
        File file = new File("F:\\美女\\1.jpg");
        //建立数据的输入通道
        FileInputStream fileInputStream = new FileInputStream(file);
        //建立缓冲数组配合循环读取文件的数据。
        int length = 0; //保存每次读取到的字节个数。
        byte[] buf = new byte[1024]; //存储读取到的数据缓冲数组的长度一般是1024的倍数。理论上缓冲数组越大,效率越高

        while((length = fileInputStream.read(buf))!=-1){ // read方法如果读取到了文件的末尾,那么会返回-1表示。
            System.out.print(new String(buf,0,length));
        }
        //关闭资源
        fileInputStream.close();

        long endTime = System.currentTimeMillis();
        System.out.println("读取的时间是:"+ (endTime-startTime)); //
    }
    //方式3:使用缓冲数组读取。    缺点: 无法读取完整一个文件的数据。     
    public static void readTest3() throws IOException{
        //找到目标文件
        File file = new File("F:\\a.txt");
        //建立数据的输入通道
        FileInputStream fileInputStream = new FileInputStream(file);
        //建立缓冲字节数组,读取文件的数据。
        byte[] buf = new byte[1024];  //相当于超市里面的购物车。
        int length = fileInputStream.read(buf); // 如果使用read读取数据传入字节数组,那么数据是存储到字节数组中的,而这时候read方法的返回值是表示的是本次读取了几个字节数据到字节数组中。
        System.out.println("length:"+ length);
        //使用字节数组构建字符串
        String content = new String(buf,0,length);
        System.out.println("内容:"+ content);
        //关闭资源
        fileInputStream.close();
    }
    //方式2 : 使用循环读取文件的数据
    public static void readTest2() throws IOException{
        long startTime = System.currentTimeMillis();
        //找到目标文件
        File file = new File("F:\\美女\\1.jpg");
        //建立数据的输入通道
        FileInputStream fileInputStream = new FileInputStream(file);
        //读取文件的数据
        int content = 0; //声明该变量用于存储读取到的数据
        while((content = fileInputStream.read())!=-1){
            System.out.print((char)content);
        }
        //关闭资源
        fileInputStream.close();
        long endTime = System.currentTimeMillis();
        System.out.println("读取的时间是:"+ (endTime-startTime)); //
    }
    //读取的方式一缺陷: 无法读取完整一个文件的数据.
    public static void readTest1() throws IOException{
        //1. 找到目标文件
        File file = new File("F:\\a.txt");
        //建立数据的输入通道。
        FileInputStream fileInputStream = new FileInputStream(file);
        //读取文件中的数据
        int content = fileInputStream.read(); // read() 读取一个字节的数据,把读取的数据返回。
        System.out.println("读到的内容是:"+ (char)content);
        //关闭资源    实际上就是释放资源。 
         fileInputStream.close();
    }

问题1: 读取完一个文件的数据的时候,我不关闭资源有什么影响?
答案: 资源文件一旦 使用完毕应该马上释放,否则其他的程序无法对该资源文件进行其他的操作。

注意:缓冲数组每次读进去的数组是覆盖不是删除后重写,所以构建String的时候要加上length


FileOutputStream

输出字节流:

——–| OutputStream 是所有输出字节流 的父类。 抽象类
———–| FileOutStream 向文件输出数据的输出字节流。

FileOutputStream如何使用呢?

1. 找到目标文件
2. 建立数据的输出通道。
3. 把数据转换成字节数组写出。
4. 关闭资源

FileOutputStream要注意的细节:

  1. 使用FileOutputStream 的时候,如果目标文件不存在,那么会自动创建目标文件对象。
  2. 使用FileOutputStream写数据的时候,如果目标文件已经存在,那么会先清空目标文件中的数据,然后再写入数据。
  3. 使用FileOutputStream写数据的时候, 如果目标文件已经存在,需要在原来数据基础上追加数据的时候应该使用new FileOutputStream(file,true)构造函数,第二参数为true。
  4. 使用FileOutputStream的write方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的只是一个字节的数据,只是把低八位的二进制数据写出,其他二十四位数据全部丢弃。

    00000000-000000000-00000001-11111111 511
    11111111—> -1

//使用字节数组把数据写出。
    public static void writeTest3() throws IOException{
        //找到目标文件
        File file = new File("F:\\b.txt");
        //建立数据输出通道
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        //把数据写出。
        String data = "abc";
        byte[] buf = data.getBytes();
        fileOutputStream.write(buf, 0, 3); // 0 从字节数组的指定索引值开始写, 2:写出两个字节。

        //关闭资源
        fileOutputStream.close();
    }

    //使用字节数组把数据写出。
    public static void writeTest2() throws IOException{
        //找到目标文件
        File file = new File("F:\\b.txt");
        //建立数据输出通道
        FileOutputStream fileOutputStream = new FileOutputStream(file,true);
        //把数据写出。
        String data = "\r\nhello world";
        fileOutputStream.write(data.getBytes());
        //关闭资源
        fileOutputStream.close();
    }
    //每次只能写一个字节的数据出去。
    public static void writeTest1() throws IOException{
        //找到目标文件
        File file = new File("F:\\b.txt");
        //建立数据的输出通道
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        //把数据写出
        fileOutputStream.write('h');
        fileOutputStream.write('e');
        fileOutputStream.write('l');
        fileOutputStream.write('l');
        fileOutputStream.write('o');
        //关闭资源
        fileOutputStream.close();   
    }

拷贝文件案例

关闭资源原则:关闭资源原则: 先开后关,后开先关

    public static void main(String[] args) throws IOException {
        //找到目标文件
        File inFile = new File("F:\\美女\\1.jpg");
        File destFile = new File("E:\\1.jpg");
        //建立数据的输入输出通道
        FileInputStream fileInputStream = new  FileInputStream(inFile);
        FileOutputStream fileOutputStream = new FileOutputStream(destFile); //追加数据....

        //每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。 每写出一次,指向都会出现相应移动。
        //建立缓冲数据,边读边写
        byte[] buf = new byte[1024]; 
        int length = 0 ; 
        while((length = fileInputStream.read(buf))!=-1){ //最后一次只剩下了824个字节
            fileOutputStream.write(buf,0,length); //写出很多次数据,所以就必须要追加。
        }
        //关闭资源原则: 先开后关,后开先关。
        fileOutputStream.close();
        fileInputStream.close();
    }

IO异常的处理

为什么要使用throw 异常:1.要停止后面的代码运行 2. 通知调用者
为什么用runtime包装:因为一般的异常要处理而且要throws,runtime可以不用throws,方便
为什么要判断FileInputStream是不是为空:因为文件可能不存在
为什么关闭两个资源要分开写(如下):防止一个失败影响另一个流的关闭

// 拷贝图片
    public static void copyImage() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            // 找到目标文件
            File inFile = new File("F:\\美女\\1.jpg");
            File outFile = new File("E:\\1.jpg");
            // 建立输入输出通道
            fileInputStream = new FileInputStream(inFile);
            fileOutputStream = new FileOutputStream(outFile);
            // 建立缓冲数组,边读边写
            byte[] buf = new byte[1024];
            int length = 0;
            while ((length = fileInputStream.read(buf)) != -1) {
                fileOutputStream.write(buf, 0, length);
            }
        } catch (IOException e) {
            System.out.println("拷贝图片出错...");
            throw new RuntimeException(e);
        } finally {
            // 关闭资源
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                    System.out.println("关闭输出流对象成功...");
                }
            } catch (IOException e) {
                System.out.println("关闭输出流资源失败...");
                throw new RuntimeException(e);
            } finally {
                if (fileInputStream != null) {
                    try {
                        fileInputStream.close();
                        System.out.println("关闭输入流对象成功...");
                    } catch (IOException e) {
                        System.out.println("关闭输入流对象失败...");
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
    public static void readTest() {
        FileInputStream fileInputStream = null;
        try {
            // 找到目标文件
            File file = new File("F:\\aaaaa.txt");
            // 建立数据输入通道
            fileInputStream = new FileInputStream(file);
            // 建立缓冲数组读取数据
            byte[] buf = new byte[1024];
            int length = 0;
            while ((length = fileInputStream.read(buf)) != -1) {
                System.out.print(new String(buf, 0, length));
            }
        } catch (IOException e) {
            /*
             * //处理的代码... 首先你要阻止后面的代码执行,而且要需要通知调用者这里出错了... throw new
             * RuntimeException(e);
             * //把IOException传递给RuntimeException包装一层,然后再抛出,这样子做的目的是
             * 为了让调用者使用变得更加灵活。
             */
            System.out.println("读取文件资源出错....");
            throw new RuntimeException(e);
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                    System.out.println("关闭资源成功...");
                }
            } catch (IOException e) {
                System.out.println("关闭资源失败...");
                throw new RuntimeException(e);
            }
        }
    }

BufferedInputStream

我们清楚读取文件数据使用缓冲数组读取效率更高,sun也知道使用缓冲数组读取效率更高,那么这时候sun给我们提供了一个——缓冲输入字节流对象,让我们可以更高效率读取文件。

输入字节流体系:
—-| InputStream 输入字节流的基类。 抽象
———-| FileInputStream 读取文件数据的输入字节流
———-| BufferedInputStream 缓冲输入字节流 缓冲输入字节流的出现主要是为了提高读取文件数据的效率。
其实该类内部只不过是维护了一个8kb的字节数组而已。

注意: 凡是缓冲流都不具备读写文件的能力

使用BufferedInputStream的步骤 :

    1. 找到目标文件。
    2. 建立数据的输入通道
    3. 建立缓冲输入字节流流
    4. 关闭资源
//构造函数
BufferedInputStream(InputStream in)//
BufferedInputStream(InputStream in, int size)//
        File file = new File("F:\\a.txt");
        FileInputStream fileInputStream= new FileInputStream(file);
        BufferedInputStream bufferedInputStream= new BufferedInputStream(fileInputStream);
        int content = 0;
        while(content = bufferedInputStream.read()!=-1)
        {
        System.out.println((char)content);
        }
        bufferedInputStream.close();

疑问一:为什么创建BufferedInputStream的时候需要传递FileInputStream
因为BufferedInputStream不具有读写文件功能,因此只需要关相应的InputStream,自带的close功能也是这样

疑问二:BufferedInputStream出现 的目的是了提高读取文件的效率,但是BufferedInputStream的read方法每次读取一个字节的数据 , 而FileInputStreram每次也是读取一个字节的数据,那meBufferedInputStream效率高从何而来?

原因:读内存的速度比读硬盘的速度快
这里写图片描述

问题三:使用少的原因:因为自己也可以定义缓冲数组,效率还更高。


BufferedOutputStream

输出字节流
——–| OutputStream 所有输出字节流的基类 抽象类
————| FileOutputStream 向文件 输出数据 的输出字节流
————| Bufferedoutputstream 缓冲输出字节流 BufferedOutputStream出现的目的是为了提高写数据的效率。 内部也是维护了一个8kb的字节数组而已。

使用BufferedOutputStream的步骤:

1. 找到目标文件
2. 建立数据的输出通道

BufferedOutputStream 要注意的细节

  1. 使用BufferedOutStream写数据的时候,它的write方法是是先把数据写到它内部维护的字节数组中。
  2. 使用BufferedOutStream写数据的时候,它的write方法是是先把数据写到它内部维护的字节数组中,如果需要把数据真正的写到硬盘上面,需要调用flush方法或者是close方法、 或者是内部维护的字节数组已经填满数据的时候。
public static void main(String[] args) throws IOException {
        //找到目标文件
        File file = new File("F:\\a.txt");
        //建立数据的输出通道
        FileOutputStream  fileOutputStream = new FileOutputStream(file);
        //建立缓冲输出字节流对象
        BufferedOutputStream bufferedOutputStream  = new BufferedOutputStream(fileOutputStream);
        //把数据写出
        bufferedOutputStream.write("hello world".getBytes()); 
        //把缓冲数组中内部的数据写到硬盘上面。
        //bufferedOutputStream.flush();
        bufferedOutputStream.close();
    }

拷贝图片

read方法注意事项:
1.如果传入了缓冲数组,内容是存储到缓冲数组中,返回值是存储到缓冲数组中的字节个数。
2.如果使用read方法没有传入缓冲数组,那么返回值是读取到的内容。

public static void main(String[] args) throws IOException {
        //找到目标文件
        File  inFile = new File("F:\\美女\\1.jpg");
        File outFile = new File("E:\\1.jpg");
        //建立数据输入输出通道
        FileInputStream fileInputStream = new FileInputStream(inFile);
        FileOutputStream fileOutputStream = new FileOutputStream(outFile);
        //建立缓冲输入输出流
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        //边读边写
        int content = 0; 
        // int length = bufferedInputStream.read(buf);   
        while((content = bufferedInputStream.read())!=-1){ 
            bufferedOutputStream.write(content);
//          bufferedOutputStream.flush();
        }
        //关闭资源
        bufferedInputStream.close();
        bufferedOutputStream.close();

码表

ASCII: 美国标准信息交换码。用一个字节的7位可以表示。
ISO8859-1: 拉丁码表。欧洲码表,用一个字节的8位表示。又称Latin-1(拉丁编码)或“西欧语言”。ASCII码是包含的仅仅是英文字母,并且没有完全占满256个编码位置,所以它以ASCII为基础,在空置的0xA0-0xFF的范围内,加入192个字母及符号,
藉以供使用变音符号的拉丁字母语言使用。从而支持德文,法文等。因而它依然是一个单字节编码,只是比ASCII更全面。
GB2312: 英文占一个字节,中文占两个字节.中国的中文编码表。
GBK: 中国的中文编码表升级,融合了更多的中文文字符号。
Unicode: 国际标准码规范,融合了多种文字。所有文字都用两个字节来表示,Java语言使用的就是unicode。
UTF-8:最多用三个字节来表示一个字符。(我们以后接触最多的是iso8859-1、gbk、utf-8)
查看上述码表后,很显然中文的‘中’在iso8859-1中是没有对映的编码的。或者一个字符在2中码表中对应的编码不同,例如有一些字在不同的编码中是有交集的,例如bjg5 和gbk 中的汉字简体和繁体可能是一样的,就是有交集,但是在各自码表中的数字不一样。

例如 :
使用gbk 将中文保存在计算机中,中 国对映 100 200 如果使用big5 打开可能 ? … 不同的编码对映的是不一样的。
很显然,我们使用什么样的编码写数据,就需要使用什么样的编码来对数据。
ISO8859-1:一个字节
GBK: 两个字节包含了英文字符和扩展的中文 ISO8859-1+中文字符
UTF-8 万国码表,推行的。是1~3个字节不等长。英文存的是1个字节,中文存的是3个字节,是为了节省空间。

注意:我们用到的FileOutputStream默认的是GBK码表

FileReader

  1. 字节流是不能写中文的(不能编码和解码)
    使用字节流写中文。 字节流之所以能够写中文是因为借助了字符串的getBytes方法对字符串进行了编码(字符—->数字)(即写入的是编码后的)

  2. 字节流:字节流读取的是文件中的二进制数据,读到的数据并不会帮你转换成你看得懂的字符。
    字符流: 字符流会把读取到的二进制的数据进行对应 的编码与解码工作。 字符流 = 字节流 + 编码(解码)
    输入字符流:
    ———-| Reader 输入字符流的基类 抽象类
    ————-| FileReader 读取文件的输入字符流。
    FileReader的用法:

    1. 找到目标文件
    2. 建立数据的输入通道
    3. 读取数据
    4. 关闭资源

    read()方法:每次只会读取一个字符

        public static void readTest1() throws IOException{
            //找到目标文件
            File file = new File("F:\\1208project\\day21\\src\\day21\\Demo1.java");
            //建立数据的输入通道
            FileReader fileReader = new FileReader(file);
            int content = 0 ;
            while((content = fileReader.read())!=-1){ //每次只会读取一个字符,效率低。
                System.out.print((char)content);
            }
            //关闭资源
            fileReader.close();
        }

    采取缓冲字符数组读取:

    public static void readTest2() throws IOException{
            //找到目标文件
            File file = new File("F:\\1208project\\day21\\src\\day21\\Demo1.java");
            // 建立数据的输入通道
            FileReader fileReader = new FileReader(file);
            //建立缓冲字符数组读取文件数据
            char[] buf = new char[1024];
            int length = 0 ; 
            while((length = fileReader.read(buf))!=-1){
                System.out.print(new String(buf,0,length));
            }
        }

FileWriter

  1. 输出字符流:
    ——| Writer 输出字符流的基类。 抽象类
    ———–| FileWriter 向文件数据数据的输出字符流

  2. FileWriter的使用步骤:

    1. 找到目标文件。
    2. 建立数据输出通道
    3. 写出数据。
    4. 关闭资源
  3. FileWriter要注意的事项:

    1. 使用FileWriter写数据的时候,FileWriter内部是维护了一个1024个字符数组的,写数据的时候会先写入到它内部维护的字符数组中,如果需要把数据真正写到硬盘上,需要调用flush或者是close方法或者是填满了内部的字符数组
    2. 使用FileWriter的时候,如果目标文件不存在,那么会自动创建目标文件。
    3. 使用FileWriter的时候, 如果目标文件已经存在了,那么默认情况会先情况文件中的数据,然后再写入数据 , 如果需要在原来的基础上追加数据,需要使用“new FileWriter(File , boolean)”的构造方法,第二参数为true。
  4. 重要方法:write(String);

    public static void  writeTest1() throws IOException{
            //找到目标文件
            File file = new File("F:\\a.txt");
            //建立数据输出通道
            FileWriter fileWriter = new FileWriter(file,true);
            //准备数据,把数据写出
            String data = "今天天气非常好!!";
            fileWriter.write(data);  //字符流具备解码的功能。
            //刷新字符流
    //      fileWriter.flush();
            //关闭资源
            fileWriter.close();//close也会调用flush 
        }
  5. 案例:拷贝文件
    注意:在拷贝图片的时候数据会丢失(两个字节两个字节查码表,找到后还是返回该二进制数字,找不到对应的字符,会返回一个未知的一个字符,是一个字节的)
    这里写图片描述

    public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new FileReader("F:\\Test.txt"));
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("E:\\Test.exe"));
            String line=null;
            while((line = bufferedReader.readLine())!=null){
            bufferedWriter.write(line);
            }
            bufferedWriter.close();
            bufferedReader.close();
        }
  6. 使用场景
    何时使用字符流,何时使用字节流?依据是什么?

    使用字符流的应用场景: 如果是读写字符数据的时候则使用字符流。

    使用字节流的应用场景: 如果读写的数据都不需要转换成字符的时候,则使用字节流。

BufferedReader

  1. 输入字符流:
    ——-| Reader 所有输入字符流的基类。 抽象类
    ———-| FileReader 读取文件字符串的输入字符流。
    ———-| BufferedReader 缓冲输入字符流 。 缓冲 输入字符流出现的目的是为了提高读取文件 的效率和拓展了FileReader的功能。 其实该类内部也是维护了一个字符数组

    记住:缓冲流都不具备读写文件的能力。

    BufferedReader的使用步骤:
    1.找到目标文件
    2 .建立数据的输入通道。

  2. 使用
    使用BUfferedReader拓展的功能,readLine() 一次读取一行文本,如果读到了文件的末尾返回null表示
    (缓冲输入字节流可以使用自己定义的字节数组来代替它提供的缓冲作用,所以并不常用,但是缓冲输入字符流添加了新的方法,所以会经常用到)

    public static void readTest1() throws IOException{
            //找到目标文件
            File file  = new File("F:\\a.txt");
            //建立数据的输入通道。
            FileReader fileReader = new FileReader(file);
            //建立缓冲输入字符流
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            //读取数据
            /*int content = bufferedReader.read();  //读到了一个字符。 读取到的字符肯定也是从Bufferedreader内部的字符数组中获取的到。所以效率高。
            System.out.println((char)content);*/
            String line =  null;
            while((line = bufferedReader.readLine())!=null){ 
                System.out.println(Arrays.toString("aaa".getBytes()));
            }
            //关闭资源
            bufferedReader.close();
        }

    虽然readLine每次读取一行数据,但是读入的line是不包含\r\n的

  3. 模拟实现readline
    public static void main(String[] args) throws IOException {
//       readTest1();
        File file  = new File("F:\\1208project\\day21\\src\\cn\\itcast\\buffered\\Demo1.java");
        //建立数据的输入通道。
        FileReader fileReader = new FileReader(file);
        String line =  null;

        while((line = myReadLine(fileReader))!=null){
            System.out.println(line);
        }
    }
    //自己去实现readLine方法。
    public static String myReadLine(FileReader fileReader) throws IOException{
        //创建一个字符串缓冲类对象
        StringBuilder sb = new StringBuilder();  //StringBuilder主要是用于存储读取到的数据
        int content = 0 ;
        while((content = fileReader.read())!=-1){
            if(content=='\r'){
                continue;
            }else if(content=='\n'){
                break;
            }else{
                //普通字符
                sb.append((char)content);
            }
        }
        //代表已经读取完毕了。
        if(content ==-1){
            return null;
        }
        return sb.toString();  
    }

BufferedWriter

案例:

public static void main(String[] args) throws IOException {
        //找到目标文件
        File file = new File("F:\\a.txt");
        //建立数据的输出通道
        FileWriter fileWriter = new FileWriter(file,true);
        //建立缓冲输出流对象
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 
        //写出数据
//      bufferedWriter.newLine(); //newLine() 换行。 实际上就是想文件输出\r\n.
        bufferedWriter.write("\r\n");
        bufferedWriter.write("前两天李克强来萝岗!!");
        //关闭资源
        bufferedWriter.flush();
//      bufferedWriter.close();

    }

IO流的继承关系图

  1. 字节流
    这里写图片描述

  2. 字符流
    这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值