基于Guava实现的文件复制

需求:现需要将文件D:\A\B\C\abc.txt进行一下操作

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

   2.将目标文件D:\A\B\C\abc.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);
    }

 

posted @ 2017-04-01 16:34  lijianfeng2017  阅读(961)  评论(0编辑  收藏  举报