JDK1.7 Paths,Files类实现文件夹的复制与删除的实例

2025-05-29 0 44

实例如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73
public static void copyFolder(String srcFolder, String destFolder)

throws IOException {

long startTime = System.currentTimeMillis();

final Path srcPath = Paths.get(srcFolder);

// 这里多创建一级,就解决了没有外壳的问题

final Path destPath = Paths.get(destFolder, srcPath.toFile().getName());

// 检查源文件夹是否存在

if (Files.notExists(srcPath)) {

System.err.println("源文件夹不存在");

System.exit(1);

}

// 如果目标目录不存在,则创建

if (Files.notExists(destPath)) {

Files.createDirectories(destPath);

}

// 这里是官方例子的开头,可能是针对大文件处理设置的参数

// Files.walkFileTree(srcPath, EnumSet.of(FileVisitOption.FOLLOW_LINKS),

// Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {}

//简化后的开头

Files.walkFileTree(srcPath, new SimpleFileVisitor<Path>() {

// 官方还调用了专门的文件夹处理,这里没使用

// 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));

// 如果说父路径不存在,则创建

if (Files.notExists(dest.getParent())) {

Files.createDirectories(dest.getParent());

}

Files.copy(file, dest);

return FileVisitResult.CONTINUE;

}

});

long endTime = System.currentTimeMillis();

System.out.println("复制成功!耗时:" + (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 //构成了一个内部类

// 处理文件

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");}

如有问题,还请提出,谢谢!

以上这篇JDK1.7 Paths,Files类实现文件夹复制删除的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/u011165335/article/details/50510531

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 JDK1.7 Paths,Files类实现文件夹的复制与删除的实例 https://www.kuaiidc.com/113900.html

相关文章

发表评论
暂无评论