Search code examples
javaniosymlinkunc

java nio iterate files in symbolic link


I would like to iterate over files that are in network UNC path so that I can operate over them, is it possible?

I'm trying in below way(see the below code) and it's not listing the files.

But, with windows explorer I can access that folder and I can see, modify even remove them.

// created with this command: mklink /D C:\Users\user\Desktop\repo \\serverIp\public\repo
File repo = new File("C:\\Users\\user\\Desktop\\repo"); // symlink

final Path dir = FileSystems.getDefault().getPath(repo.getCanonicalPath());

Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path newDir, BasicFileAttributes attrs) throws IOException {
        System.out.println(newDir.toAbsolutePath());                
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        System.out.println(file.toAbsolutePath());
        return FileVisitResult.CONTINUE;
    }
});

Did I create the symbolic link correctly?


Solution

  • I think you need to use FOLLOW_LINKS option.

    import static java.nio.file.FileVisitResult.*;
    Files.walkFileTree(dir, EnumSet.of(FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { ... ))