12 javaIO流---文件拷贝【字节流实现】

在这里插入图片描述个人介绍


大家好我是:一颗松
认真分享技术,记录学习点滴
如果分享对你有用请支持我哦🍺

点赞:👍 留言:✍收藏:⭐️

个人格言: 想法落实的最佳时机就是现在!🏄


在这里插入图片描述

javaIO流—文件拷贝🖨️

💡 通过IO流实现文件拷贝的思路:
①读取源文件
②把读到数据写入新文件

1.1 图文解析

🌰 java IO流拷贝文件案例(字节流实现)

需求:将指定目录下的文件拷贝到另一个目录下


public class FileCopy {
    public static void main(String[] args) {
        //    任务要求:完成文件拷贝
//        将E盘aba目录下的a.txt文件拷贝到 abb目录下

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        //定义路径
        String path1 = "E:/aba/a.txt";
        String path2 = "E:/abb/b.txt";

        try {
//            创建对象
            fileInputStream = new FileInputStream(path1);
            fileOutputStream = new FileOutputStream(path2);
//            读取文件
//                定义一个字节数组,提高读取效率
            byte buf []= new byte[1024];
            int readLend = 0;
            while ((readLend=fileInputStream.read(buf)) != -1){
//                读取到后就写入path2,通过fileOutputStream写
                fileOutputStream.write(buf,0,readLend);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (fileInputStream != null){
//                    关闭输入流资源                    

                    fileInputStream.close();
                }
                if (fileOutputStream != null){
//                    关闭输出流资源                    
                    fileOutputStream.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        }
    }

  • 18
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 27
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zinksl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值