spring常用工具类,引入spring即可使用
boolean deleteRecursively(@Nullable File root) 删除文件
boolean deleteSuccess = FileSystemUtils.deleteRecursively(new File("D:\\fileTest\\b\\新建文件夹\\shuihe.sql"));
System.out.println(deleteSuccess);
boolean deleteRecursively(@Nullable Path root) 递归删除目录及下所有文件及目录
Path src = Paths.get("D:\\fileTest\\a");
boolean deleteSuccess = FileSystemUtils.deleteRecursively(src);
System.out.println(deleteSuccess);
void copyRecursively(File src, File dest) 复制文件到另外一个文件
File src = new File("D:\\fileTest\\b\\新建文件夹\\huidiao - 副本.txt");
File dest = new File("D:\\fileTest\\b\\新建文件夹\\huidiao.txt");
FileSystemUtils.copyRecursively(src, dest);
注意:复制的文件存在,会报错 Exception in thread "main" java.nio.file.FileAlreadyExistsException: D:\fileTest\b\新建文件夹\huidiao.txt
void copyRecursively(Path src, Path dest) 递归复制目录下的所有文件及目录到另一个目录下,存在的文件会被覆盖
Path src = Paths.get("D:\\fileTest\\a");
Path dest = Paths.get("D:\\fileTest\\b");
FileSystemUtils.copyRecursively(src, dest);
Path的使用,可参考微信文章:Java 文件处理 Paths & Files