The correct way to use FileVisitor in java

后端 未结 3 2208
再見小時候
再見小時候 2021-01-01 00:34

I am trying to step through an entire path and its single layer of sub directories. For each file, I need to read five data fields and output them to a delimited text file.

3条回答
  •  温柔的废话
    2021-01-01 01:11

    Now to answer your revised post...

    You have a bracket in the wrong place:

    Files.walkFileTree(startPath, new SimpleFileVisitor(startPath) { 
          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
              throws IOException {
              Files.list(file);
              return FileVisitResult.CONTINUE;
          }
      });
    

    I moved the bracket after new SimpleFileVisitor(startPath) to enclose the visitFile method. What you have here is an anonymous class - that's where you provide an implementation "on the fly".

提交回复
热议问题