【Java】Input,Output,Stream I/O流 02

Reader & Writter 只适合文本的输入输出 【.txt .java .c .cpp】

传输文件,不能使用文本IO进行读写,需要使用文件输入输出流

public class IOTest {
    public static void main(String[] args) throws Exception {
        // 文件输入输出

        // 图片文件测试复制
        File srcImg = new File("1774415.jpg");
        // 新图片文件
        File destImg = new File("new.jpg");

        // 写入对象
        InputStream inputStream = new FileInputStream(srcImg);

        // 写出对象
        OutputStream outputStream = new FileOutputStream(destImg);

        // 缓冲字节数组
        byte[] bytesBuffer = new byte[10];
        // 返回个数
        int len;
        while ( (len = inputStream.read(bytesBuffer)) != -1){
            // 写入数据
            outputStream.write(bytesBuffer,0,len);
        }
        inputStream.close();
        outputStream.close();
    }
}

 

文件复制的方法封装

    static void fileCopy(String srcPath,String destPath){ // 文件复制的方法封装
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try{
            inputStream = new FileInputStream(srcFile);
            outputStream = new FileOutputStream(destFile);
            byte[] bytesBuffer = new byte[1024]; //2的十次方
            int length; // 当前读取个数
            while ((length = inputStream.read(bytesBuffer)) != -1){
                outputStream.write(bytesBuffer,0,length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

缓冲流,提高文件读写效率

- BufferedInputStream

- BufferedOutputStream

- BufferedReader

- BufferedWritter

    static void bufferFileCopy(String srcPath,String destPath){ // 文件复制的方法封装
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);
        
        InputStream inputStream = null;
        OutputStream outputStream = null;
        
        InputStream bufferInput = null;
        OutputStream bufferOutput = null;
        
        try{
            inputStream = new FileInputStream(srcFile);
            outputStream = new FileOutputStream(destFile);
            
            // 多添加一次缓冲流对象装载
            bufferInput = new BufferedInputStream(inputStream);
            bufferOutput = new BufferedOutputStream(outputStream);
            
            // 调用缓冲输入输出读写文件
            byte[] bytesBuffer = new byte[1024]; //2的十次方
            int length; // 当前读取个数
            while ((length = bufferInput.read(bytesBuffer)) != -1){
                bufferOutput.write(bytesBuffer,0,length);
          // bufferOutput.flush(); 刷新缓冲数组,但是write已经自带此方法了,所以不用再调用了 } } catch (Exception e) { e.printStackTrace(); } finally { try {
          // 先关闭缓冲输入输出对象 bufferInput.close(); bufferOutput.close(); // 在关闭文件输入输出对象 关闭外层流的同时,内存流也会被关闭,这一步可以省略 //outputStream.close(); //inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }

 

- BufferedReader 和 BufferedWritter 也是一样套接实现的流对象上

    static void bufferFileCopy(String srcPath,String destPath){ // 文件复制的方法封装
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        Reader reader = null;
        Writer writer = null;

        BufferedReader bufferReader = null;
        BufferedWriter bufferWriter = null;

        try{
            reader = new FileReader(srcFile);
            writer = new FileWriter(destFile);

            // 多添加一次缓冲流对象装载
            bufferReader = new BufferedReader(reader);
            bufferWriter = new BufferedWriter(writer);

            // 调用缓冲输入输出读写文件
            /*byte[] bytesBuffer = new byte[1024]; //2的十次方
            int length; // 当前读取个数
            while ((length = bufferInput.read(bytesBuffer)) != -1){
                bufferOutput.write(bytesBuffer,0,length);
            }*/

            // 采用String读取
            String data;
            while( (data = bufferReader.readLine()) != null){
                System.out.println(data);
                bufferWriter.write(data + "\n");// data 不包含换行字符 或者bufferWriter.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bufferReader.close();
                bufferWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

转换流

- InputStreamReader 一个字节输入流 转换至 字符输入流

- OutputStreamWriter 一个字节输出流 转换至 字符输出流

解码 : 字节 字节数组 -> 字符串 字符数组

编码: 字符串 字符数组 -> 字节 字节数组

- 字符集问题  https://www.bilibili.com/video/BV12741127qm

ASCII 美国标准信息交换码,阿斯克码

GBK 中国中文编码表升级,兼容更多中文,原GB2312

Unicode 国际标准码

UTF-8 中英通用

    static void readFile(String srcPath) throws Exception {
        File file = new File(srcPath);
        InputStream inputStream = new FileInputStream(file);
        InputStreamReader inputReader = new InputStreamReader(inputStream,"UTF-8");
        // new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        // new InputStreamReader(inputStream, "UTF-8"); 一般不写字符集参数,默认使用系统字符集[文件创建时,设置的字符集]
        char[] charsBuffer = new char[12];
        int len;
        while ((len = inputReader.read(charsBuffer)) != -1 ){
            String str = new String(charsBuffer,0,len);
            System.out.println(str);
        }
        inputReader.close();
    }

文件字符转换的输入输出问题

    static void transFileCopy(String srcPath,String destPath) throws Exception {
        // 创建文件、创建流对象
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        InputStream inputStream = new FileInputStream(srcFile);
        OutputStream outputStream = new FileOutputStream(destFile);

        InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"GBK");

        // 读写过程
        char[] charsBuffer = new char[10];
        int len;
        while ((len = inputStreamReader.read(charsBuffer)) != -1){
            outputStreamWriter.write(charsBuffer,0,len);
        }

        // 释放资源
        inputStreamReader.close();
        outputStreamWriter.close();
    }

 

上一篇:设计模式之美学习-结构型-装饰者模式(二十)


下一篇:Spring源码学习 ---Spring解析XML过程