简体   繁体   English

如何知道FileVisitor中访问最后一个文件的时间?

[英]How to know when the last file is being visited in FileVisitor?

I need to do something with the file visited last in a directory. 我需要对目录中访问过的文件做一些事情。 How can I know if the current call to my visitFile() is the last one? 我怎么知道当前对visitFile()调用是否是最后一次?

(I only want to list all the files and directories in a given directory. To do so, I've introduced a depth field to my FileVisitor implementation and in the preVisitDirectory I return SKIP_SUBTREE if the depth is greater than 0. (And then increment the depth .) The problem is that I don't know when to reset the depth to 0, because when I call the walkFileTree with this FileVisitor implementation for another directory, the depth is already > 0 and it only lists the given directory.) (我只想列出指定目录下的所有文件和目录。要做到这一点,我介绍了一个depth领域我FileVisitor实施,并在preVisitDirectory我回到SKIP_SUBTREE如果深度大于0(然后递增depth 。)问题是我不知道何时将depth重置为0,因为当我用另一个目录的FileVisitor实现调用walkFileTree时, depth已经> 0并且它只列出给定的目录。)

How about maintaining the depth only within the two methods, preVisitDirectory and postVisitDirectory ? 如何仅在两个方法中保持深度, preVisitDirectorypostVisitDirectory You'll increment depth in preVisitDirectory and decrement it in postVisitDirectory . 你会增加depthpreVisitDirectory和递减它postVisitDirectory You might have to initialize depth to -1 , to have depth == 0 when in the start directory though. 您可能必须将depth初始化为-1 ,以便在开始目录中具有depth == 0 That way, you'll always have the right depth . 这样,你将永远拥有正确的depth

Edit: If you return SKIP_SIBLINGS from visitFile , instead of from preVisitDirectory , the postVisitDirectory will still get called! 编辑:如果从visitFile返回SKIP_SIBLINGS ,而不是从preVisitDirectory ,则仍会调用postVisitDirectory

Here's a code sample: 这是一个代码示例:

public class Java7FileVisitorExample {

public void traverseFolder(Path start){
    try {
        Files.walkFileTree(start, new SimpleFileVisitor<Path>() {

            private int depth = -1;

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                    throws IOException {
                System.out.println("preVisitDirectory(" + dir + ")");
                depth++;
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                    throws IOException {
                if (depth > 0) {
                    return FileVisitResult.SKIP_SIBLINGS;
                }

                System.out.println("visitFile(" + file + ", " + attrs + "): depth == " + depth);

                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException e)
                    throws IOException {
                if (e == null) {
                    depth--;
                    System.out.println("postVisitDirectory(" + dir + ")");
                    return FileVisitResult.CONTINUE;
                } else {
                    throw e;
                }


            }
        });
    } catch (IOException ex) {
        Logger.getAnonymousLogger().throwing(getClass().getName(), 
                "traverseFolder", ex);
    }
}

public static void main(String... args) {
    Path start = Paths.get("/Book/Algorithm");
    new Java7FileVisitorExample().traverseFolder(start);
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保存上次访问的活动并在开始时启动它 - How to save last visited activity and launch it on start 如何知道窗口小部件何时被渲染? - How to know when a widget is being rendered? 我怎么知道HashMap中的所有值都被访问过一次? - How should I get know that all values in HashMap is visited once? 从文件读取时如何仅修复检查用户名和密码的数组的最后一行 - How to fix only last line of array being checked for username and password when read from a file 我反射时如何知道最后一个物体? - How can I know the last object when I reflect? 是否可以在不存在的文件上调用Java的FileVisitor.visitFile()? - Is it possible for Java's FileVisitor.visitFile() to be called on a nonexistent file? Scala:特征扩展了java.nio.file.FileVisitor - Scala: trait extends java.nio.file.FileVisitor 如何将for循环迭代回到它在java中访问的最后一个索引 - How to iterate back a for loop to the last index it visited in java 如何知道使用Eclipse编译时传递的命令 - How to know the commands being passed when compiling with eclipse 当我尝试将每条记录写入同一个文件时,只有最后一条记录被写入 json 文件 - Only last record is being written to the json File when I am trying to write every record in the same file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM