Java复制文件

Java复制文件

private boolean copyFile(String oldFile,String newFile){
		//定义输出/入流
		InputStream is = null;
		OutputStream os = null;
		StringBuilder stringBuilder = new StringBuilder();

		try {
			is = new FileInputStream(oldFile);
			os = new FileOutputStream(newFile);
			byte[] flush = new byte[1024];
			int len;
			while ((len = is.read(flush)) != -1){
				os.write(flush,0,len);
			}
			os.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//关闭流注意事项 先开启的后关闭
			try {
				if (null != os){
					os.close();
				}
				if (null != is){
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (new File(newFile).exists()){
			return true;
		}
		return false;
	}

复制文件夹

import java.io.*;
import java.time.Duration;
import java.time.Instant;

public class CopyDir {
    public static void main(String[] args) {
        Instant start = Instant.now();
        boolean isOk = dirOrFile("要复制的目录路径", "目标路径");
        if(isOk){
            System.out.println("目录复制完成!");
        }else {
            System.out.println("目录复制失败!");
        }
        Instant end = Instant.now();
        System.out.println("耗时:"+Duration.between(start,end).toMillis()+"毫秒!");
    }

	private static boolean copyDir(String oldDir, String newDir) {
        //定义流
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(oldDir);
            os = new FileOutputStream(newDir);
            //定义缓冲字节数组
            byte[] flush = new byte[1024];
            //定义读取长度
            int len;
            while ((len = is.read(flush)) != -1) {
                os.write(flush, 0, len);
            }
            //刷新流
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            try {
                if (null != os) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (null != is) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

	private static boolean dirOrFile(String oldDir, String newDir) {
        //定义写入写出文件
        File old = new File(oldDir);
        File copy = new File(newDir);
        //判断如果是目录的话 创建目录
        if (old.isDirectory()) {
            copy.mkdir();
            File[] files = old.listFiles();
            for (int i = 0; i < files.length; i++) {
                dirOrFile(files[i].getAbsolutePath(), newDir + "/" + files[i].getName());
            }
        }
        if (old.isFile()) {
            copyDir(oldDir, newDir);
        }
        if (old.length()==copy.length()){
            return true;
        }
        return false;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值