IO(File_Input_OutPut)-字节流(不推荐,效率低)

1- 对"字节流"的使用

"字节流"PK"字符流",最大的优点--->(这章的"字节流",可以复制图片和视频)
//字节流
//优点,可以读视频和图片
public class File_InPut_OutPut {
    @Test
    // 复制"图片"
    public void test1() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //1- 读,写的两个文件-实例化
            File srcFile = new File("方园.jpeg");
            File destFile = new File("方园1.jpeg");

            //2- 给读取,写出--一个流
            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(destFile);
            //3- 复制操作
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }
            System.out.println("复制成功---Success");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4- 关闭流

            if (fileInputStream != null)
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            if (fileOutputStream != null)
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }
    }

    //指定路径下的文件复制(----- 方法的封装)
    public void copyFile(String srcPath, String destPath) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //1- 读,写的两个文件-实例化
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);

            //2- 给读取,写出--一个流
            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(destFile);
            //3- 复制操作
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }
            System.out.println("复制成功---Success");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4- 关闭流

            if (fileInputStream != null)
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            if (fileOutputStream != null)
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }
    }

    @Test //(对上面,封装方法的调用)
    public void Copy() {
        long start = System.currentTimeMillis();
        copyFile("方园.jpeg", "方园2.jpeg");
        long end = System.currentTimeMillis();
        //执行了多少秒
        System.out.println("复制一份文件,执行了 = " + (end - start) + " 秒");

    }
}

posted on   陈嘻嘻-  阅读(29)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示