java将一个文件夹得文件读取后复制到另一个文件夹中

这是一个Java程序,用于批量读取指定目录下的文件,并将它们上传到另一个目录。程序首先定义了读取和写入文件的路径,然后通过split方法处理路径数组,确保读取和写入的目录数量相匹配。对于每个读取的文件,它会检查是否为文件,创建目标目录(如果不存在),并使用FileCopyUtils.copy方法复制文件,同时记录日志信息。如果上传成功,源文件会被删除。
摘要由CSDN通过智能技术生成

还可以写入多个文件夹目录,读取上传都可以

package com.esatcom.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import java.io.*;
import java.util.Objects;
import static org.springframework.util.FileCopyUtils.copy;

@Slf4j
public class upload {

    //读取文件的路径
    private static String readFilesPath = "C:\\task\\readpath";

    //上传文件的路径
    private static String writeFilesPath = "C:\\task\\writepath";

    public static void main(String[] args) {
        String[] readPathList = readFilesPath.split(",");
        String[] writePathList = writeFilesPath.split(",");
        int length = 0;
        if (writePathList.length >= readPathList.length) {
            length = readPathList.length;
        } else if (writePathList.length < readPathList.length) {
            length = writePathList.length;
        }
        for (int i = 0; i < length; i++) {
            readAndWrite(readPathList[i], writePathList[i]);
        }
    }

    private static void readAndWrite(String read, String write) {
        log.info("读取文件START");
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        File file = new File(read);
        if (file.isDirectory()) {
            File[] filePathLists = file.listFiles();
            for (int i = 0; i < Objects.requireNonNull(filePathLists).length; i++) {
                if (filePathLists[i].isFile()) {
                    if (!new File(write).exists()) {
                        new File(write).mkdir();
//java中File类自带一个检测方法exists可以判断文件或文件夹是否存在,一般与mkdirs方法(该方法相较于mkdir可以创建包括父级路径,推荐使用该方法)或者createNewFile方法合作使用。
                        log.info("未获取到符合上传条件的文件目录,创建目录:" + new File(write).getName());
                    }
                    boolean upload = false;
                    try {
                        FileInputStream fis = new FileInputStream(filePathLists[i]);
                        FileOutputStream fos = new FileOutputStream(new File(write, filePathLists[i].getName()));
                        copy(fis, fos);
                        fis.close();
                        fos.close();
                        filePathLists[i].delete();
                        upload = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.info("上传文件异常");
                    }
                    if (upload) {
                        log.info("上传文件:" + filePathLists[i].getName() + "成功");
                    } else {
                        log.info("上传文件:" + filePathLists[i].getName() + "失败");
                    }

                }
            }
        } else {
            log.info("未获取到符合读取条件的文件目录");
        }
        stopWatch.stop();
        log.info("读取并上传文件END,耗时:{} S", stopWatch.getTime());
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jiettt-_-

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

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

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

打赏作者

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

抵扣说明:

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

余额充值