复习IO流-复制文件

/*
 使用字节流将D盘视频文件传到C盘other文件下
 */
public class Class {
    public static void main(String[] args) throws IOException {
        //第一步 读取D盘里的视频文件

        String path = "D:\\A\\B\\me.mp4";
        File file = new File(path);
        /*
        构造方法摘要
FileInputStream(File file)
          通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
FileInputStream(String name)
          通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
         */
        FileInputStream fileInputStream = new FileInputStream(file);
        /*
        构造方法摘要
           BufferedInputStream(InputStream in)
          创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
         */
        String lastPath = "C:\\Other\\me.mp4";
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(lastPath));
        //读取该路径的文件。
        /*
        read()
          从此输入流中读取一个数据字节。
 int read(byte[] b)
          从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
 int read(byte[] b, int off, int len)
          从此输入流中将最多 len 个字节的数据读入一个 byte 数组中

         */
        /*
         规定一次读取1024个byte
         */
        byte b [] = new byte[1024];
        int len;
        //无参的read()方法返回的int类型,是表示数据下一个字节的字节码
         while ((len=fileInputStream.read(b))!=-1)
         {
             //把读取的文件写入C盘other文件下
//             void write(byte[] b, int off, int len)
//             将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此缓冲的输出流。
//             void write(int b)
//             将指定的字节写入此缓冲的输出流。
                        //这里一定要写len 不然如果有1039个 一次读1024 剩下的15个就读不出来 会造成文件损失
             bufferedOutputStream.write(b,0,len);


         }
        System.out.println("复制成功");
         //写完关闭流

        bufferedInputStream.close();
         bufferedOutputStream.close();


    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值