java common file_java使用commons.io的FileUtils进行文件拷贝

参考:

官方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);

}

原文:https://www.cnblogs.com/flypig666/p/12342933.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值