参考:
官方api:
maven引入依赖:
commons-io
commons-io
2.5
相关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 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 voidCopeFile(String name) {
String path=Constant.IMG_PATH;
File srcFIle= newFile(path);
String editionPath= path +name;
File targetFile= newFile(editionPath);
File[] files=srcFIle.listFiles();for(File item : files) {if(item.isFile()) {try{
FileUtils.copyFileToDirectory(item, targetFile);
}catch(IOException e) {
e.printStackTrace();
}
}
}
}public static voidDeleteFile(String name) {
String path=Constant.IMG_PATH;
String editionPath= path +name;
File targetFile= newFile(editionPath);
FileUtils.deleteQuietly(targetFile);
}