JDK1.7 Paths,Files類實現(xiàn)文件夾的復(fù)制與刪除的實例
更新時間:2017年11月27日 10:46:53 作者:尚云峰111
下面小編就為大家分享一篇JDK1.7 Paths,Files類實現(xiàn)文件夾的復(fù)制與刪除的實例,具有很好的參考價值,希望對大家有所幫助。以前跟隨小編過來看看吧
實例如下所示:
public static void copyFolder(String srcFolder, String destFolder)
throws IOException {
long startTime = System.currentTimeMillis();
final Path srcPath = Paths.get(srcFolder);
// 這里多創(chuàng)建一級,就解決了沒有外殼的問題
final Path destPath = Paths.get(destFolder, srcPath.toFile().getName());
// 檢查源文件夾是否存在
if (Files.notExists(srcPath)) {
System.err.println("源文件夾不存在");
System.exit(1);
}
// 如果目標目錄不存在,則創(chuàng)建
if (Files.notExists(destPath)) {
Files.createDirectories(destPath);
}
// 這里是官方例子的開頭,可能是針對大文件處理設(shè)置的參數(shù)
// Files.walkFileTree(srcPath, EnumSet.of(FileVisitOption.FOLLOW_LINKS),
// Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {}
//簡化后的開頭
Files.walkFileTree(srcPath, new SimpleFileVisitor<Path>() {
// 官方還調(diào)用了專門的文件夾處理,這里沒使用
// public FileVisitResult preVisitDirectory(Path dir,
// BasicFileAttributes attrs) throws IOException {return null;}
@Override
// 文件處理,將文件夾也一并處理,簡潔些
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
Path dest = destPath.resolve(srcPath.relativize(file));
// 如果說父路徑不存在,則創(chuàng)建
if (Files.notExists(dest.getParent())) {
Files.createDirectories(dest.getParent());
}
Files.copy(file, dest);
return FileVisitResult.CONTINUE;
}
});
long endTime = System.currentTimeMillis();
System.out.println("復(fù)制成功!耗時:" + (endTime - startTime) + "ms");
}
// 刪除文件夾
public static void deleteFolder(String Foleder) throws IOException {
Path start = Paths.get(Foleder);
if (Files.notExists(start)) {
throw new IOException("文件夾不存在!");
}
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override //構(gòu)成了一個內(nèi)部類
// 處理文件
public FileVisitResult visitFile(Path file,BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
// 再處理目錄
public FileVisitResult postVisitDirectory(Path dir, IOException e)
throws IOException {
if (e == null) {
Files.delete(dir);
return FileVisitResult.CONTINUE;
} else {
throw e;
}
}
});
System.out.println("刪除成功!");
}
public static void main(String[] args) throws IOException {
//copyFolder("C:\\Users\\Administrator\\Desktop\\111", "D:\\壓縮\\1級\\2級");// 419ms,378ms,429ms....
deleteFolder("C:\\Users\\Administrator\\Desktop\\111");}
如有問題,還請?zhí)岢?,謝謝!
以上這篇JDK1.7 Paths,Files類實現(xiàn)文件夾的復(fù)制與刪除的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot?DataSource數(shù)據(jù)源實現(xiàn)自動配置流程詳解
這篇文章主要介紹了SpringBoot?DataSource數(shù)據(jù)源實現(xiàn)自動配置流程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10
SpringBoot 使用hibernate validator校驗
這篇文章主要介紹了SpringBoot 使用hibernate validator校驗,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
在Linux系統(tǒng)上升級Java版本的兩種方法步驟
由于項目升級,需要將JDK7升級到JDK8,升級JDK的同時也要升級一些其他的版本,下面這篇文章主要給大家介紹了關(guān)于在Linux系統(tǒng)上升級Java版本的兩種方法步驟,需要的朋友可以參考下2024-09-09
java 利用反射機制,獲取實體所有屬性和方法,并對屬性賦值
這篇文章主要介紹了 java 利用反射機制,獲取實體所有屬性和方法,并對屬性賦值的相關(guān)資料,需要的朋友可以參考下2017-01-01
Springboot打包為Docker鏡像并部署的實現(xiàn)
這篇文章主要介紹了Springboot打包為Docker鏡像并部署的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Spring boot創(chuàng)建自定義starter的完整步驟
這篇文章主要給大家介紹了關(guān)于Spring boot創(chuàng)建自定義starter的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Spring boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Java設(shè)計模式之訪問模式(Visitor者模式)介紹
這篇文章主要介紹了Java設(shè)計模式之訪問模式(Visitor者模式)介紹,本文講解了為何使用Visitor模式、如何使用Visitor模式、使用Visitor模式的前提等內(nèi)容,需要的朋友可以參考下2015-03-03

