org.apache.commons.io.FileUtils工具类各方法使用详解,二次封装

/**
 * 将url内容复制到文件
 *
 * @param source            url
 * @param destination       文件目录
 * @param connectionTimeout 连接超时,单位毫秒
 * @param readTimeout       读取超时,单位毫秒
 * @throws IOException
 */
public static void copyURLToFile(String source, String destination, int connectionTimeout, int readTimeout) throws IOException {
    org.apache.commons.io.FileUtils.copyURLToFile(new URL(source), new File(destination), connectionTimeout, readTimeout);
}

/**
 * 将文件A的内容复制到文件B
 *
 * @param fromPath
 * @param toPath
 * @throws IOException
 */
public static void copyToFile(String fromPath, String toPath) throws IOException {
    InputStream inputStream;
    File file = new File(toPath);
    inputStream = new FileInputStream(fromPath);//用字节流的方式cope文件
    org.apache.commons.io.FileUtils.copyToFile(inputStream, file);
}

/**
 * 将文件A的复制到文件B
 *
 * @param fromPath
 * @param toPath
 * @param preserveFileDate 是否保留日期,true保留日期,false不保留日期
 * @throws IOException
 */
public static void copyFile(String fromPath, String toPath, boolean preserveFileDate) throws IOException {
    org.apache.commons.io.FileUtils.copyFile(new File(fromPath), new File(toPath), preserveFileDate);
}

/**
 * 将文件复制到其他文件夹中
 *
 * @param sourceFile
 * @param destinationDir
 * @param preserveFileDate 是否保留日期,true保留日期,false不保留日期
 * @throws IOException
 */
public static void copyFileToDirectory(String sourceFile, String destinationDir, boolean preserveFileDate) throws IOException {
    org.apache.commons.io.FileUtils.copyFileToDirectory(new File(sourceFile), new File(destinationDir), preserveFileDate);
}

/**
 * 复制文件夹
 *
 * @param srcDir           源文件夹
 * @param destDir          目标文件夹
 * @param preserveFileDate 是否保留日期,true保留日期,false不保留日期
 */
public static void copyDirectory(String srcDir, String destDir, boolean preserveFileDate) throws IOException {
    org.apache.commons.io.FileUtils.copyDirectory(new File(srcDir), new File(destDir), preserveFileDate);
}

/**
 * 在给定目录(及其子目录)中查找与扩展名数组匹配的文件
 *
 * @param folderPath 文件夹路径
 * @param suffix     要查找的文件后缀名
 * @param b          是否查找子目录
 * @return 符合条件的文件路径
 */
public static File[] listFiles_fileSuffix(String folderPath, String[] suffix, boolean b) {
    File file = new File(folderPath);
    return org.apache.commons.io.FileUtils.convertFileCollectionToFileArray(org.apache.commons.io.FileUtils.listFiles(file, suffix, b));
}

/**
 * 在给定目录(及其子目录)中查找文件
 *
 * @param folderPath    文件夹路径
 * @param fileName      要查找的文件名
 * @param subfolderName 如果需要在子目录查找,输入子目录的文件夹名,不需要的话传入null
 * @return 符合条件的文件路径
 */
public static File[] listFiles_fileName(String folderPath, String fileName, String subfolderName) {
    File file = new File(folderPath);
    if (subfolderName == null) {
        return org.apache.commons.io.FileUtils.convertFileCollectionToFileArray(org.apache.commons.io.FileUtils.listFiles(file, FileFilterUtils.nameFileFilter(fileName), null));
    }
    return org.apache.commons.io.F
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

萌翻天

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

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

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

打赏作者

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

抵扣说明:

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

余额充值