基于Guava实现的文件复制

需求:现需要将文件D:ABCabc.txt进行一下操作

   1.在文件夹D:ABC下,没有以abc命名的文件夹则创建

   2.将目标文件D:ABCabc.txt复制到abc下

实现代码:

/**
     * 以目标文件名创建文件夹,并将目标文件复制到该文件夹下
     *
     * @param srcFilePath 原文件路径
     * @throws Exception Exception
     */
    public static void copyFileToSub(String srcFilePath) throws Exception {
        File srcFile = new File(srcFilePath);
        //文件全名(如:demo.txt)
        String simplePath = Files.simplifyPath(srcFile.getName());
        //不带后缀名文件名(如:demo)
        String fileName = Files.getNameWithoutExtension(simplePath);
        //获取父级路径名
        String parentPath = srcFile.getParent();
        //组装目标文件路径
        String destFilePath = parentPath + File.separator + fileName + File.separator + simplePath;

        File destFile = new File(destFilePath);
        //创建目标文件父级目录
        Files.createParentDirs(destFile);

        Files.copy(srcFile, destFile);
    }
原文地址:https://www.cnblogs.com/watson-ljf/p/6656532.html