Last active
June 7, 2023 14:16
-
-
Save ov7a/a0c529843149278d36e2b7017631390d to your computer and use it in GitHub Desktop.
WalkTree link visiting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ls -lFR | |
total 0 | |
lrwxr-xr-x 1 ov7a staff 8 Jun 7 18:15 link@ -> original | |
drwxr-xr-x 3 ov7a staff 96 Jun 7 18:15 original/ | |
./original: | |
total 8 | |
-rw-r--r-- 1 ov7a staff 9 Jun 7 18:15 original.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
import java.io.IOException; | |
import java.nio.file.*; | |
import java.nio.file.attribute.BasicFileAttributes; | |
import java.util.EnumSet; | |
public class Main { | |
public static void main(String[] args) { | |
Path rootDir; | |
try { | |
rootDir = setup(); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
FileVisitor<Path> pathVisitor = new PathVisitor(); | |
try { | |
Files.walkFileTree(rootDir, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, pathVisitor); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private static Path setup() throws IOException { | |
Path rootDir = Paths.get("tmp"); | |
Path original = rootDir.resolve("original"); | |
Files.createDirectories(original); | |
Files.write(original.resolve("original.txt"), "some text".getBytes()); | |
Files.createSymbolicLink(rootDir.resolve("link"), rootDir.relativize(original)); | |
return rootDir; | |
} | |
public static class PathVisitor implements FileVisitor<Path> { | |
@Override | |
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { | |
System.out.println("Visiting dir:" + dir); | |
return FileVisitResult.CONTINUE; | |
} | |
@Override | |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | |
System.out.println("Visiting file:" + file); | |
return FileVisitResult.CONTINUE; | |
} | |
@Override | |
public FileVisitResult visitFileFailed(Path file, IOException exc) { | |
System.out.println("Visiting file failed" + file); | |
return FileVisitResult.CONTINUE; | |
} | |
@Override | |
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { | |
return FileVisitResult.CONTINUE; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> Task :Main.main() | |
Visiting dir:tmp | |
Visiting dir:tmp/original | |
Visiting file:tmp/original/original.txt | |
Visiting file:tmp/link |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment