简体   繁体   中英

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?

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>() { ... ))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM