栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

IO流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

IO流

目录
    • 1 IO流的介绍
      • 1.1 为什么要学习IO流
      • 1.2 什么是IO流
      • 1.3 IO流的分类
    • 2 字节流输出流
      • 2.1 字节输出流入门
          • FileOutputStream类 :
          • 构造方法 :
          • 字节输出流写数据快速入门
      • 2.2 字节输出流写数据的方法
      • 2.3 写数据的换行和追加写入
    • 3 字节输入流
      • 3.1 字节输入流介绍
          • 字节输入流类
          • 构造方法
          • 步骤
      • 3.2 字节输入流读多个字节
      • 3.3 图片的拷贝
      • 3.4 异常的捕获处理
          • JDK7版本之前处理方式 : 手动释放资源
          • JDK7版本优化处理方式 : 自动释放资源
          • 代码实践
      • 3.4 字节输入流一次读一个字节数组
    • 4 字节缓冲区流
      • 4.1 字节缓冲流概述
      • 4.2 字节缓冲流案例
      • 4.3 缓冲流一次读写一个字节原理
      • 4.4 缓冲流一次读写一个字节数组原理
    • 5 Properties集合
      • 5.1 Properties集合的概述
      • 5.2 Properties作为集合的特有方法
      • 5.3 properties中和IO相关的方法
    • 6 ResourceBundle工具类
    • 7 递归

1 IO流的介绍 1.1 为什么要学习IO流
  • 通过变量,数组,或者集合存储数据
    • 都是不能永久化存储 , 因为数据都是存储在内存中
    • 只要代码运行结束,所有数据都会丢失
  • 使用IO流
    • 1,将数据写到文件中,实现数据永久化存储
    • 2,把文件中的数据读取到内存中(Java程序)
1.2 什么是IO流
  • I 表示input ,是数据从硬盘进内存的过程,称之为读。
  • O 表示output ,是数据从内存到硬盘的过程。称之为写。
  • IO的数据传输,可以看做是一种数据的流动,按照流动的方向,以内存为参照物,进行读写操作
    • 简单来说:内存在读,内存在写
1.3 IO流的分类
  • 按照流向区分
    • 输入流 : 用来读数据
    • 输出流 : 用来写数据
  • 按照类型区分
    • 字节流
    • 字符流
  • 注意 :
    • 字节流可以操作任意文件 (所有类型文件) —>音频、视频、图片
    • 字符流只能操作 纯文本文件 -->java、go文件
    • 用windows记事本打开能读的懂,那么这样的文件就是纯文本文件。
2 字节流输出流 2.1 字节输出流入门
  • FileOutputStream类 :
    • OutputStream有很多子类,我们从最简单的一个子类开始。
    • java.io.FileOutputStream 类是文件输出流,用于将数据写出到文件
  • 构造方法 :
    • public FileOutputStream(File file):创建文件输出流以写入由指定的 File对象表示的文件。
    • public FileOutputStream(String name): 创建文件输出流以指定的名称写入文件。
    public class FileOutputStreamConstructor throws IOException {
        public static void main(String[] args) {
       	 	// 使用File对象创建流对象
            File file = new File("a.txt");
            FileOutputStream fos = new FileOutputStream(file);
          
            // 使用文件名称创建流对象
            FileOutputStream fos = new FileOutputStream("b.txt");
        }
    }
    
  • 字节输出流写数据快速入门
    • 创建字节输出流对象
    • 写数据
    • 释放资源
    package com.itheima.outputstream_demo;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    
    public class OutputStreamDemo1 {
        public static void main(String[] args) throws IOException {
            // 创建字节输出流对象
            // 如果指定的文件不存在 , 会自动创建文件
            // 如果文件存在 , 会把文件中的内容清空
            FileOutputStream fos = new FileOutputStream("day11_demo\a.txt");
    
            // 写数据
            // 写到文件中就是以字节形式存在的
            // 只是文件帮我们把字节翻译成了对应的字符 , 方便查看
            fos.write(97);
            fos.write(98);
            fos.write(99);
    
            // 释放资源
            // while(true){}
            // 断开流与文件中间的关系
            fos.close();
        }
    }
    
2.2 字节输出流写数据的方法
  • 字节流写数据的方法

    • 1 void write(int b) 一次写一个字节数据
    • 2 void write(byte[] b) 一次写一个字节数组数据
    • 3 void write(byte[] b, int off, int len) 一次写一个字节数组的部分数据
    package com.itheima.outputstream_demo;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    
    public class OutputStreamDemo2 {
        public static void main(String[] args) throws IOException {
            // 创建字节输出流对象
            FileOutputStream fos = new FileOutputStream("day11_demo\a.txt");
    
            // 写数据
    //        1 void write​(int b)	一次写一个字节数据
            fos.write(97);
            fos.write(98);
            fos.write(99);
    
    //        2 void write​(byte[] b)	一次写一个字节数组数据
            byte[] bys = {65, 66, 67, 68, 69};
            fos.write(bys);
    
    //        3 void write​(byte[] b, int off, int len)	一次写一个字节数组的部分数据
            fos.write(bys, 0, 3);
    
            // 释放资源
            fos.close();
        }
    }
    
2.3 写数据的换行和追加写入
package com.itheima.outputstream_demo;

import java.io.FileOutputStream;
import java.io.IOException;


public class OutputStreamDemo3 {
    public static void main(String[] args) throws IOException {
        // 创建字节输出流对象
        FileOutputStream fos = new FileOutputStream("day11_demo\a.txt");

        // void write(int b)  一次写一个字节数据
        fos.write(97);
        // 因为字节流无法写入一个字符串 , 把字符串转成字节数组写入
        fos.write("rn".getBytes());
        fos.write(98);
        fos.write("rn".getBytes());
        fos.write(99);
        fos.write("rn".getBytes());

        // 释放资源
        fos.close();
    }
}
package com.itheima.outputstream_demo;

import java.io.FileOutputStream;
import java.io.IOException;


public class OutputStreamDemo3 {
    public static void main(String[] args) throws IOException {
        // 创建字节输出流对象
        // 追加写数据
        // 通过构造方法 : public FileOutputStream​(String name,boolean append) : 追加写数据
        FileOutputStream fos = new FileOutputStream("day11_demo\a.txt" , true);

        // void write​(int b)	一次写一个字节数据
        fos.write(97);
        // 因为字节流无法写入一个字符串 , 把字符串转成字节数组写入
        fos.write("rn".getBytes());
        fos.write(98);
        fos.write("rn".getBytes());
        fos.write(99);
        fos.write("rn".getBytes());

        // 释放资源
        fos.close();
    }
    // 写完数据换行操作
    private static void method1() throws IOException {
        // 创建字节输出流对象
        FileOutputStream fos = new FileOutputStream("day11_demo\a.txt");

        // void write​(int b)	一次写一个字节数据
        fos.write(97);
        // 因为字节流无法写入一个字符串 , 把字符串转成字节数组写入
        fos.write("rn".getBytes());
        fos.write(98);
        fos.write("rn".getBytes());
        fos.write(99);
        fos.write("rn".getBytes());

        // 释放资源
        fos.close();
    }
}

3 字节输入流 3.1 字节输入流介绍
  • 字节输入流类
    • InputStream类 : 字节输入流最顶层的类 , 抽象类
      — FileInputStream类 : FileInputStream extends InputStream
  • 构造方法
    • public FileInputStream(File file) : 从file类型的路径中读取数据
    • public FileInputStream(String name) : 从字符串路径中读取数据
  • 步骤
    • 创建输入流对象
    • 读数据
    • 释放资源
  • package com.itheima.inputstream_demo;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    
    
    public class FileInputStreamDemo1 {
        public static void main(String[] args) throws IOException {
            // 创建字节输入流对象
            // 读取的文件必须存在 , 不存在则报错
            FileInputStream fis = new FileInputStream("day11_demo\a.txt");
    
            // 读数据 , 从文件中读到一个字节
            // 返回的是一个int类型的字节
            // 如果想看字符, 需要强转
            int by = fis.read();
            System.out.println((char) by);
    
            // 释放资源
            fis.close();
        }
    }
    
3.2 字节输入流读多个字节
package com.itheima.inputstream_demo;

import java.io.FileInputStream;
import java.io.IOException;


public class FileInputStreamDemo2 {
    public static void main(String[] args) throws IOException {
        // 创建字节输入流对象
        // 读取的文件必须存在 , 不存在则报错
        FileInputStream fis = new FileInputStream("day11_demo\a.txt");

        // 读数据 , 从文件中读到一个字节
        // 返回的是一个int类型的字节
        // 如果想看字符, 需要强转
//        int by = fis.read();
//        System.out.println(by);
//        by = fis.read();
//        System.out.println(by);
//        by = fis.read();
//        System.out.println(by);
//
//        by = fis.read();
//        System.out.println(by);
//        by = fis.read();
//        System.out.println(by);
//        by = fis.read();
//        System.out.println(by);

        // 循环改进
        int by;// 记录每次读到的字节
        while ((by = fis.read()) != -1) {
            System.out.print((char) by);
        }

        // 释放资源
        fis.close();
    }
}
3.3 图片的拷贝
package com.itheima.inputstream_demo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class FileInputStreamDemo2 {
    public static void main(String[] args) throws IOException {
        // 创建字节输入流对象
        FileInputStream fis = new FileInputStream("D:\传智播客\安装包\好看的图片\liqin.jpg");

        // 创建字节输出流
        FileOutputStream fos = new FileOutputStream("day11_demo\copy.jpg");

        // 一次读写一个字节
        int by;
        while ((by = fis.read()) != -1) {
            fos.write(by);
        }

        // 释放资源
        fis.close();
        fos.close();
    }
}

3.4 异常的捕获处理
  • JDK7版本之前处理方式 : 手动释放资源
    package com.itheima.inputstream_demo;
    
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    
    public class FileInputStreamDemo4 {
        public static void main(String[] args) {
            FileInputStream fis = null ;
            FileOutputStream fos = null;
            try {
                // 创建字节输入流对象
                fis = new FileInputStream("D:\传智播客\安装包\好看的图片\liqin.jpg");
    
                // 创建字节输出流
                fos = new FileOutpu	tStream("day11_demo\copy.jpg");
    
                // 一次读写一个字节
                int by;
                while ((by = fis.read()) != -1) {
                    fos.write(by);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 释放资源
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                // 释放资源
                if(fos != null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    
  • JDK7版本优化处理方式 : 自动释放资源
    • JDK7优化后可以使用 try-with-resource 语句 , 该语句确保了每个资源在语句结束时自动关闭。
      简单理解 : 使用此语句,会自动释放资源 , 不需要自己在写finally代码块了

    • 注意:使用前提是资源的类型必须是AutoCloseable接口的实现类

    • 格式 :

      格式 :  
      try (创建流对象语句1 ; 创建流对象语句2 ...) {
              // 读写数据
          } catch (IOException e) {
              处理异常的代码...
          }
      
      举例 :
          try ( 
              FileInputStream fis1 = new FileInputStream("day11_demo\a.txt") ; 
      	    FileInputStream fis2 = new FileInputStream("day11_demo\b.txt") ) 
          {
              // 读写数据
          } catch (IOException e) {
              处理异常的代码...
          }
      
      
  • 代码实践
    package com.itheima.inputstream_demo;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    
    public class FileInputStreamDemo5 {
        public static void main(String[] args) {
            try (
                    // 创建字节输入流对象
                    FileInputStream fis = new FileInputStream("D:\传智播客\安装包\好看的图片\liqin.jpg");
                    // 创建字节输出流
                    FileOutputStream fos = new FileOutputStream("day11_demo\copy.jpg")
            ) {
                // 一次读写一个字节
                int by;
                while ((by = fis.read()) != -1) {
                    fos.write(by);
                }
                // 释放资源 , 发现已经灰色 , 提示多余的代码 , 所以使用 try-with-resource 方式会自动关流
                // fis.close();
                // fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
3.4 字节输入流一次读一个字节数组
  • FileInputStream类 :

    • public int read(byte[] b) : 从输入流读取最多b.length个字节的数据, 返回的是真实读到的数据个数
    package com.itheima.inputstream_demo;
    
    
    import javax.sound.midi.Soundbank;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    
    public class FileInputStreamDemo6 {
        public static void main(String[] args) throws IOException {
            // 创建字节输入流对象
            FileInputStream fis = new FileInputStream("day11_demo\a.txt");
    
    //        public int read​(byte[] b):
    //        1 从输入流读取最多b.length个字节的数据
    //        2 返回的是真实读到的数据个数
    
            byte[] bys = new byte[3];
    
    //        int len = fis.read(bys);
    //        System.out.println(len);// 3
    //        System.out.println(new String(bys));// abc
    //
    //        len = fis.read(bys);
    //        System.out.println(len);// 2
    //        System.out.println(new String(bys));// efc
    
            System.out.println("==========代码改进===============");
    
    //        int len = fis.read(bys);
    //        System.out.println(len);// 3
    //        System.out.println(new String(bys, 0, len));// abc
    //
    //        len = fis.read(bys);
    //        System.out.println(len);// 2
    //        System.out.println(new String(bys, 0, len));// ef
    
            System.out.println("==========代码改进===============");
    
            int len;
            while ((len = fis.read(bys)) != -1) {
                System.out.print(new String(bys , 0 , len));
            }
    
            fis.close();
        }
    }
    
  • 对复制图片的代码进行使用一次读写一个字节数组的方式进行改进

    package com.itheima.inputstream_demo;
    
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    
    public class FileInputStreamDemo7 {
        public static void main(String[] args) throws IOException {
            // 创建字节输入流对象
            FileInputStream fis = new FileInputStream("D:\传智播客\安装包\好看的图片\liqin.jpg");
            // 创建字节输出流
            FileOutputStream fos = new FileOutputStream("day11_demo\copy.jpg");
    
            byte[] bys = new byte[1024];
            int len;// 每次真实读到数据的个数
            int by;
            while ((len = fis.read(bys)) != -1) {
                fos.write(bys, 0, len);
            }
    
            // 释放资源
            fis.close();
            fos.close();
        }
    }
    
    
4 字节缓冲区流 4.1 字节缓冲流概述
  • 字节缓冲流:

    • ​ BufferOutputStream:缓冲输出流
    • ​ BufferedInputStream:缓冲输入流
  • 构造方法:

    • ​ 字节缓冲输出流:BufferedOutputStream(OutputStream out)
    • ​ 字节缓冲输入流:BufferedInputStream(InputStream in)
  • 为什么构造方法需要的是字节流,而不是具体的文件或者路径呢?

    • ​ 字节缓冲流仅仅提供缓冲区,不具备读写功能 , 而真正的读写数据还得依靠基本的字节流对象进行操作
4.2 字节缓冲流案例
package com.itheima.bufferedstream_demo;

import java.io.*;


public class BufferedStreamDemo1 {
    public static void main(String[] args) throws IOException {
        // 创建高效的字节输入流对象
        // 在底层会创建一个长度为8192的数组
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\传智播客\安装包\好看的图片\liqin.jpg"));
        // 创建高效的字节输出流
        // 在底层会创建一个长度为8192的数组
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day11_demo\copy.jpg"));

        // 使用高效流 , 一次读写一个字节
        int by;
        while ((by = bis.read()) != -1) {
            bos.write(by);
        }

//        byte[] bys = new byte[1024];
//        int len;// 每次真实读到数据的个数
//        while ((len = bis.read(bys)) != -1) {
//            bos.write(bys, 0, len);
//        }

        // 释放资源
        // 在底层会把基本的流进行关闭
        bis.close();
        bos.close();
    }
}

4.3 缓冲流一次读写一个字节原理

4.4 缓冲流一次读写一个字节数组原理

  • 四种方式复制视频文件效率对比
package com.itheima.bufferedstream_demo;

import java.awt.image.DataBufferDouble;
import java.io.*;


public class BufferedStreamDemo2 {
    public static void main(String[] args) throws IOException {
        long startTime = System.currentTimeMillis();

        // method1();
        // method2();
        // method3();
        method4();

        long endTime = System.currentTimeMillis();
        System.out.println("花费的时间为:" + (endTime - startTime) + "毫秒");
    }

    // 4 缓冲流一次读写一个字节数组
    private static void method4() throws IOException {
        // 创建高效的字节输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\a.wmv"));
        // 创建高效的字节输出流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day11_demo\copy.wmv"));

        // 一次读写一个字节数组
        byte[] bys = new byte[1024];
        int len;// 每次真实读到数据的个数
        while ((len = bis.read(bys)) != -1) {
            bos.write(bys, 0, len);
        }

        // 释放资源
        bis.close();
        bos.close();
    }

    //  3 缓冲流一次读写一个字节
    private static void method3() throws IOException {
        // 创建高效的字节输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\a.wmv"));
        // 创建高效的字节输出流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day11_demo\copy.wmv"));

        // 一次读写一个字节
        int by;
        while ((by = bis.read()) != -1) {
            bos.write(by);
        }


        // 释放资源
        bis.close();
        bos.close();
    }

    // 2 基本的字节流一次读写一个字节数组
    private static void method2() throws IOException {
        // 创建基本的字节输入流对象
        FileInputStream fis = new FileInputStream("D:\a.wmv");
        // 创建基本的字节输出流对象
        FileOutputStream fos = new FileOutputStream("day11_demo\copy.wmv");

        // 一次读写一个字节数组
        byte[] bys = new byte[1024];
        int len;// 每次真实读到数据的个数
        while ((len = fis.read(bys)) != -1) {
            fos.write(bys, 0, len);
        }

        // 释放资源
        fis.close();
        fos.close();
    }

    // 1 基本的字节流一次读写一个字节
    private static void method1() throws IOException {
        // 创建基本的字节输入流对象
        FileInputStream fis = new FileInputStream("D:\a.wmv");
        // 创建基本的字节输出流对象
        FileOutputStream fos = new FileOutputStream("day11_demo\copy.wmv");

        // 一次读写一个字节
        int by;
        while ((by = fis.read()) != -1) {
            fos.write(by);
        }

        // 释放资源
        fis.close();
        fos.close();
    }
}
5 Properties集合 5.1 Properties集合的概述