Apache commons.io

Apache Commons IO 提供了一系列用于处理文件、目录、流、过滤和转换的工具方法。本文详细介绍了如何使用 `copyFileToDirectory`、`copyDirectoryToDirectory`、`deleteDirectory` 和 `moveDirectory` 等方法进行文件和目录的复制、删除及移动操作,包括对文件过滤和安全删除的处理。
摘要由CSDN通过智能技术生成

一、Create、Modify

1、copyFileToDirectory

/**

     * Copies a file to a directory optionallypreserving the file date.

     * <p>

     * This method copies the contents of thespecified source file

 * toa file of the same name in the specified destination directory.

// destDir必须是文件夹,若不存在则会自动创建

     * The destination directory is created ifit does not exist.

     * If the destination file exists, thenthis method will overwrite it.

    */

    publicstaticvoidcopyFileToDirectory(final File srcFile, final File destDir, finalbooleanpreserveFileDate)

            throws IOException {

        if (destDir == null) {

            thrownew NullPointerException("Destination must not be null");

        }

        if (destDir.exists()&& destDir.isDirectory() == false) {

            thrownew IllegalArgumentException("Destination '" + destDir + "' is not a directory");

        }

        final File destFile = new File(destDir, srcFile.getName());

//java.io.File.File(File parent,String child)

        copyFile(srcFile, destFile, preserveFileDate);

//内部调用下面的copyFile方法

    }

 

/**

     * Copies a file to a new location.

     * <p>

     * This method copies the contents of thespecified source file

     * to the specified destination file.

     * The directory holding the destinationfile is created if it does not exist. // 目标文件父目录不存在则会创建

     * If the destination file exists, thenthis method will overwrite it.

     * // 目标文件存在则会覆盖

     */

    publicstaticvoid copyFile(final File srcFile, final File destFile,

                                finalbooleanpreserveFileDate) throws IOException {

        checkFileRequirements(srcFile, destFile);

        if (srcFile.isDirectory()) {

            thrownew IOException("Source'" + srcFile + "' exists but is a directory");

        }

        if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())){

            thrownew IOException("Source'" + srcFile + "' and destination '" + destFile + "' arethe same");

        }

        final File parentFile = destFile.getParentFile();

        if (parentFile != null) {

            if (!parentFile.mkdirs() && !parentFile.isDirectory()){

                thrownew IOException("Destination'" + parentFile + "' directory cannot be created");

            }

        }

        if (destFile.exists() && destFile.canWrite() == false) {

            thrownew IOException("Destination'" + destFile + "' exists but is read-only");

        }

        doCopyFile (srcFile, destFile, preserveFileDate);

// 内部调用doCopyFile方法实现

    }

}

 

/**

     * Internal copy file method.

     * This caches the original file length,and throws an IOException

     * if the output file length is differentfrom the current input file length. // 传输过程中文件改变会抛异常

     * So it may fail if the file changes size.

     * It may also fail with"IllegalArgumentException: Negative size" if the input file istruncated part way

     * through copying the data and the newfile size is less than the current position.

     */

    privatestaticvoid doCopyFile(final File srcFile, final File destFile, finalbooleanpreserveFileDate)

            throws IOException {

        if (destFile.exists() && destFile.isDirectory()){

            thrownew IOException("Destination'" + destFile + "' exists but is a directory");

        }

 

        try (FileInputStream fis = new FileInputStream(srcFile);

             FileChannel input = fis.getChannel();

// 内部调用NIO channel

             FileOutputStream fos = new FileOutputStream(destFile);

             FileChannel output = fos.getChannel()) {

           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值