|
| 1 | +package nio.file; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.nio.file.FileVisitResult; |
| 6 | +import java.nio.file.FileVisitor; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.nio.file.Paths; |
| 10 | +import java.nio.file.attribute.BasicFileAttributes; |
| 11 | +import java.util.function.Consumer; |
| 12 | +import metrics.Temp; |
| 13 | +import org.eclipse.jgit.api.Git; |
| 14 | +import org.eclipse.jgit.errors.RepositoryNotFoundException; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +/** |
| 18 | + * @GitHub : https://github.com/zacscoding |
| 19 | + */ |
| 20 | +public class FilesBasicTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void walkFileTree() throws IOException { |
| 24 | + /* |
| 25 | + D:\temp |
| 26 | + -- AA |
| 27 | + -- AAA |
| 28 | + -- a.txt |
| 29 | + -- BB |
| 30 | + -- BBB |
| 31 | + --- BBBB |
| 32 | + --- bbb.txt |
| 33 | + */ |
| 34 | + /* |
| 35 | + preVisitDirectory :: file:///D:/temp/ |
| 36 | + preVisitDirectory :: file:///D:/temp/AA/ |
| 37 | + visitFile :: file:///D:/temp/AA/a.txt |
| 38 | + preVisitDirectory :: file:///D:/temp/AA/AAA/ |
| 39 | + postVisitDirectory :: file:///D:/temp/AA/AAA/ |
| 40 | + postVisitDirectory :: file:///D:/temp/AA/ |
| 41 | + preVisitDirectory :: file:///D:/temp/BB/ |
| 42 | + preVisitDirectory :: file:///D:/temp/BB/BBB/ |
| 43 | + visitFile :: file:///D:/temp/BB/BBB/bbb.txt |
| 44 | + preVisitDirectory :: file:///D:/temp/BB/BBB/BBBB/ |
| 45 | + postVisitDirectory :: file:///D:/temp/BB/BBB/BBBB/ |
| 46 | + postVisitDirectory :: file:///D:/temp/BB/BBB/ |
| 47 | + postVisitDirectory :: file:///D:/temp/BB/ |
| 48 | + postVisitDirectory :: file:///D:/temp/ |
| 49 | + */ |
| 50 | + Path root = new File("D:\\temp").toPath(); |
| 51 | + Files.walkFileTree(root, new FileVisitor<Path>() { |
| 52 | + @Override |
| 53 | + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { |
| 54 | + return FileVisitResult.CONTINUE; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 59 | + System.out.println("visitFile :: " + file.toUri().toString()); |
| 60 | + return FileVisitResult.CONTINUE; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { |
| 65 | + System.out.println("visitFileFailed :: " + file.toUri().toString()); |
| 66 | + return FileVisitResult.CONTINUE; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 71 | + System.out.println("postVisitDirectory :: " + dir.toUri().toString()); |
| 72 | + return FileVisitResult.CONTINUE; |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void findGitDirectory() throws IOException { |
| 79 | + Path start = Paths.get("C:\\git\\zaccoding"); |
| 80 | + Consumer<Path> gitDirectoryConsumer = path -> System.out.println("Found git dir : " + path.toString()); |
| 81 | + GitDirectoryVisitor visitor = new GitDirectoryVisitor(start, gitDirectoryConsumer); |
| 82 | + Files.walkFileTree(start, visitor); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + private static class GitDirectoryVisitor implements FileVisitor<Path> { |
| 87 | + |
| 88 | + private Path root; |
| 89 | + private Consumer<Path> gitDirectoryConsumer; |
| 90 | + |
| 91 | + public GitDirectoryVisitor(Path root, Consumer<Path> gitDirectoryConsumer) { |
| 92 | + this.root = root; |
| 93 | + this.gitDirectoryConsumer = gitDirectoryConsumer; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { |
| 98 | + if (root == dir) { |
| 99 | + return FileVisitResult.CONTINUE; |
| 100 | + } |
| 101 | + |
| 102 | + try (Git git = Git.open(dir.toFile())) { |
| 103 | + gitDirectoryConsumer.accept(dir); |
| 104 | + return FileVisitResult.SKIP_SUBTREE; |
| 105 | + } catch (RepositoryNotFoundException e) { |
| 106 | + return FileVisitResult.CONTINUE; |
| 107 | + } catch (Exception e) { |
| 108 | + return FileVisitResult.CONTINUE; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 114 | + return FileVisitResult.CONTINUE; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { |
| 119 | + return FileVisitResult.CONTINUE; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 124 | + return FileVisitResult.CONTINUE; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments