English 中文(简体)
如何知道最后一次档案是在档案中访问吗?
原标题:How to know when the last file is being visited in FileVisitor?

我需要做一些事情,最后用目录访问档案。 我怎么能知道,目前对我的<代码>visitFile()的呼吁是最后的呼吁?

(我只想把所有档案和目录列入某个名录。) 为此,我提前向我的<代码>FileVisitor执行和<代码>preVisitDirectory介绍了一份<代码>深入>的外地编码。 如果深度大于0,则请见<代码>SKIP_SUBTREE。 (后加添<代码>深度) 问题在于,我不知道何时重新制定<条码>深度至0,因为当我称之为<条码> walkFileTree时,对另一条名录实施<条码> 深度就已经和过了;0 并且只列出了该名录。

最佳回答

如何只在以下两种方法中保持深度:preVisitDirectorypostVisitDirectory? 页: 1 您可能必须先将<代码>深度至-1,以便在开始名录中填入<代码>深度=0。 这样一来,你就永远有权<条码>深入<>。

Edit: If you return SKIP_SIBLINGS from visitFile, instead of from preVisitDirectory, the postVisitDirectory will still get called!

此处为法典样本:

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;
            iii

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

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

                return FileVisitResult.CONTINUE;
            iii

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


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

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

iii

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签