IO流详解Java

1. IO流体系

  1. 按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
  2. 按数据流的流向不同分为:输入流,输出流
  3. 按流的角色的不同分为:节点流,处理流
  4. 节点流:直接从数据源或目的地读写数据
    在这里插入图片描述
  5. 处理流:不直接连接到数据源或目的地,而是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能
    在这里插入图片描述
  6. IO流详细分类图
    在这里插入图片描述

2. 节点流的使用

  1. 读取操作
@Test
public void test2() {
    
    
    FileReader fr = null;
    try {
    
    
        // 1. 实例化File类
        File file = new File("src\\echo06\\hi.txt");


        // 2. 实例化FileReader的流
        fr = new FileReader(file);


        // 3. 读入操作
        // read(char[] cuff):返回每次读入cuff数组中的字符的个数
        char[] cuff = new char[5];
        int len;
        while ((len = fr.read(cuff)) != -1) {
    
    
            //方式一
            // 错误的
//            for (int i = 0; i < cuff.length; i++) {
    
    
//                System.out.print(cuff[i]);
//            }
            for (int i = 0; i < len; i++) {
    
    
                System.out.print(cuff[i]);
            }
            // 方式二
            // 错误的
//            String str = new String(cuff);
            String str = new String(cuff, 0, len);
            System.out.print(str);
        }
    } catch (IOException e) {
    
    
        e.printStackTrace();
    } finally {
    
    
        // 4. 关闭流
        if(fr != null) {
    
    
            try {
    
    
                fr.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
    }
}
  1. 写出操作
/**
* 输出操作:
* 1. 对应的File可以是不存在的,并不会报异常
* 2. File对应的硬盘中的文件如果不存在,在输出的过程中,会自动穿件此文件
*    File对应的硬盘中的文件如果存在:
*          如果流使用的构造器是:FileWriter(file, false)/FileWriter(file):则覆盖原文件
*          如果流使用的构造器是:FileWriter(file, true):则在原文件基础上追加
*
*/
@Test
public void test3() {
    
    
    FileWriter fw = null;
    try {
    
    
        // 1. 提供File类的对象
        File file = new File("src\\echo06\\hello.txt");


        // 2. 提供FileWriter的对象,用于数据的写出
        fw = new FileWriter(file);


        // 3. 写出操作
        fw.write("Hello world!\n");
        fw.write("Hello CHINA!");
    } catch (IOException e) {
    
    
        e.printStackTrace();
    } finally {
    
    
        // 4. 关闭流
        try {
    
    
            fw.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

3. 处理流

  1. 转换流
/**
* 1. 转换流:属予字符流
* InputStreamReader:解码:字节、字节数组 --- > 字符串
* OutputStreamWriter:编码:字符数组、字符串 -—- > 字节、字节数组
* 2. 作用:提供字节流与字符流之间的转换
*/


@Test
public void test6() {
    
    
    InputStreamReader isr = null;
    try {
    
    
        FileInputStream fis = new FileInputStream("src\\echo06\\hello.txt");
        // 参数2指明字符集,具体使用哪个字符集,取决于文件保存时使用的字符集
        isr = new InputStreamReader(fis, StandardCharsets.UTF_8);


        char[] buff = new char[13];
        int len;
        while ((len = isr.read(buff)) != -1) {
    
    
            String str = new String(buff, 0, len);
            System.out.println(str);
        }
    } catch (IOException e) {
    
    
        e.printStackTrace();
    } finally {
    
    
        try {
    
    
            assert isr != null;
            isr.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}
  1. 缓冲流
    提高读写速度
	/**
	* 实现文件的复制
	*/
    public void copyFileBuffered(String srcPath, String destPath) {
    
    
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
    
    
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);


            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);


            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);


            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
    
    
                bos.write(buffer, 0, len);
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            // 4. 资源关闭
            // 要求:先关闭外层的流,在关闭内层的流
            try {
    
    
                assert bos != null;
                bos.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
            try {
    
    
                bis.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
            // 关闭外层流的同时,内层流也会自动进行关闭
        }
    }
}

猜你喜欢

转载自blog.csdn.net/E_chos/article/details/114642315