复制拷贝文件(IO-File)

方法一 

1.编写一个FileUtil类,在里面写上coofile方法

然后将输入流读取的文件使用字符流输出

输入流

FileInputStream is = new FileInputStream(src)

输出流

FileOutputStream ou = new FileOutputStream(det)

将输入流读取的文件使用字符流输出

transferTo 方法的作用是将输入流读取的数据 使用字符输出流写出。可用于复制文件等操作。

  is.transferTo(ou);
  public static void coofile(File src ,File det){
        try(FileInputStream is = new FileInputStream(src); 
            FileOutputStream ou = new FileOutputStream(det)){
             is.transferTo(ou);
                }catch(Exception e){
                     e.printStackTrace();
                }
    }

2.创建一个类,.在main方法中进行调用 

直接调用FileUtil.coofile()方法,new两个文件,,将第一个文件复制拷贝给第二个文件

public class Stream3 {
    public static void main(String[] args) {
         //文件复制
    
       FileUtil.coofile(new File("d:/640 (2).gif"),new File("ok.gif"));
       
    }
}

 

方法二

//读取一个字节写入一个字节,速度慢

1.直接引流 

  try(FileInputStream is = new FileInputStream("ok.gif");
            FileOutputStream out = new FileOutputStream("cj.gif")){

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

2.将读取的内容用字符输出流写出

将读取的内容赋值给b;

当(b)!= -1时用字符输出流写出

按字节读取文件内容时,会做一个位运算(与0xFF做与运算),使字节范围从-128到127变成0到255,然后就可以以-1为结束了。所以不能等于-1 

 try(FileInputStream is = new FileInputStream("ok.gif");
            FileOutputStream out = new FileOutputStream("cj.gif")){

              int b= is.read();
            while((b)!=-1){
                out.write(b);
            }


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

完整代码

 

/*
 * Copyright (c) 2020, 2023.
 *
 */

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;

/**
 * <p>Project: File - Stream4</p>
 * <p>Powered by Cyy On 2023-07-21 15:45:08</p>
 * <p>描述:<p>
 *
 * @author Cyy [210585265@qq.com]
 * @version 1.0
 * @since 17
 */
public class Stream4 {
    public static void main(String[] args) {
        try(FileInputStream is = new FileInputStream("ok.gif");
            FileOutputStream out = new FileOutputStream("cj.gif")){
           
           //读取一个字节写入一个字节,速度慢
            int b= is.read();
            while((b)!=-1){
                out.write(b);
            }
         }catch(Exception e){
                      e.printStackTrace();
                 }


    }
}

 

方法三(建议使用)

//文件复制 自己设置传输速度  快

1.直接引流

  try(FileInputStream is = new FileInputStream("ok.gif");
            FileOutputStream out = new FileOutputStream("cj.gif")){

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

2.读取内容到数组

后面的字节速度阔以自己设置,(不要过大,以免引起卡顿)

 byte[] bytes = new byte[10240];

3.将读取的内容用字符输出流写出 

将读取的大小先设置为0,

将读取的内容赋值给len,

当大于0时,直接写出

              int len =0;
              while ((len=is.read())>0){
                   out.write(bytes,0,len);
              }

4.完整代码

/*
 * Copyright (c) 2020, 2023.
 *
 */

import com.sun.source.tree.TryTree;

import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * <p>Project: File - Stream5</p>
 * <p>Powered by Cyy On 2023-07-21 16:17:13</p>
 * <p>描述:<p>
 *
 * @author Cyy [210585265@qq.com]
 * @version 1.0
 * @since 17
 */
public class Stream5 {
    public static void main(String[] args) {
         //文件复制  自己设置速度 快
         try(FileInputStream is = new FileInputStream("ok.gif");
             FileOutputStream out = new FileOutputStream("cj.gif")){

              byte[] bytes = new byte[10240];
              int len =0;
              while ((len=is.read())>0){
                   out.write(bytes,0,len);
              }

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

    }
}

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值