参考:
https://blog.csdn.net/maxiaokundage/article/details/40146981
https://blog.csdn.net/hardwin/article/details/7914610
https://blog.csdn.net/qq_42402854/article/details/83374559
官方api:
http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html
maven引入依赖:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
相关api:
一、拷贝文件
copyFile(File srcFile, File destFile):Copies a file to a new location preserving the file date.
copyFile(File srcFile, File destFile, boolean preserveFileDate):Copies a file to a new location.
copyFile(File input, OutputStream output):Copy bytes from a File to an OutputStream.
copyFileToDirectory(File srcFile, File destDir):Copies a file to a directory preserving the file date.
copyFileToDirectory(File srcFile, File destDir, boolean preserveFileDate):Copies a file to a directory optionally preserving the file date.
copyInputStreamToFile(InputStream source, File destination):Copies bytes from an InputStream
source
to a file destination
.
copyToDirectory(File src, File destDir):Copies a file or directory to within another directory preserving the file dates.
copyToDirectory(Iterable<File> srcs, File destDir):Copies a files to a directory preserving each file‘s date.
copyToFile(InputStream source, File destination):Copies bytes from an InputStream
source
to a file destination
.
copyURLToFile(URL source, File destination):Copies bytes from the URL source
to a file destination
.
copyURLToFile(URL source, File destination, int connectionTimeout, int readTimeout):Copies bytes from the URL source
to a file destination
.
二、拷贝目录及文件
copyDirectory(File srcDir, File destDir):Copies a whole directory to a new location preserving the file dates.
copyDirectory(File srcDir, File destDir, boolean preserveFileDate):Copies a whole directory to a new location.
copyDirectory(File srcDir, File destDir, FileFilter filter):Copies a filtered directory to a new location preserving the file dates.
copyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate):Copies a filtered directory to a new location.
copyDirectoryToDirectory(File srcDir, File destDir):Copies a directory to within another directory preserving the file dates.
三、删除目录及文件
deleteDirectory(File directory):Deletes a directory recursively.
deleteQuietly(File file):Deletes a file, never throwing an exception.
forceDelete(File file):Deletes a file.
四、清除目录
cleanDirectory(File directory):Cleans a directory without deleting it.
例子:
public void CopeFile(String name) { String path = Constant.IMG_PATH; File srcFIle = new File(path); String editionPath = path + name; File targetFile = new File(editionPath); File[] files = srcFIle.listFiles(); for (File item : files) { if (item.isFile()) { try { FileUtils.copyFileToDirectory(item, targetFile); } catch (IOException e) { e.printStackTrace(); } } } } public static void DeleteFile(String name) { String path = Constant.IMG_PATH; String editionPath = path + name; File targetFile = new File(editionPath); FileUtils.deleteQuietly(targetFile); }
java使用commons.io的FileUtils进行文件拷贝
原文:https://www.cnblogs.com/flypig666/p/12342933.html