6

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. I'm able to read from a single text file and validate my output on screen; after that I'm stuck. I cannot seem to to find the right parameters for FileVisit. Some specific questions are comments in my code posted below. And although I'm no nearly that far yes, I'd like to get some idea for writing to an output file, namely whether the place I wish to put it is the most logical one.

I've reviewed the https://stackoverflow.com/questions/9913/java-file-io-compendium and JavaDocs' info on the File Visitor
http://docs.oracle.com/javase/7/docs/api/index.html?java/nio/file/FileVisitor.html . However, I'm still not able to get FileVisitor working properly.

@Bohemian suggested changing interface to class which I've done.

 import java.nio.files.*;
 public class FileVisitor<T> 
 {
      Path startPath = Paths.get("\\CallGuidesTXT\\");
      Files.walkFileTree(startPath, new SimpleFileVisitor(startPath))
      \\             ^^^^^^    
      \\ errors out, <identifier expected>          
          { 
          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
              throws IOException
          {
              Files.list(file);
              return FileVisitResult.CONTINUE;
          }
        // do my file manipulations here, then write the delimited line 
        // of text to a CSV fle...is this the most appropriate place for that 
        // operation in this sample? 
      }  
 }

SSCCE below...but comments in the version above point to specific questions I'm having.

 import java.nio.*;
 import java.util.*;
 public class FileVisitor<T>
 {
    Path startPath = Paths.get("\\CallGuidesTXT\\");
 }
 Files.walkFileTree(startPath, new SimpleFileVisitor(startPath)  {
      @Override
      public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
          throws IOException {
          Files.list(file);
          return FileVisitResult.CONTINUE;
      } 
 });
4

3 回答 3

9

我对 Java 有点生疏,但我认为你要去的地方有一个粗略的想法:

import java.nio.files.*;
public class MyDirectoryInspector extends Object 
{
    public static void main(String[] args) {
        Path startPath = Paths.get("\\CallGuidesTXT\\");
        Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() { 
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                throws IOException
            {
                String firstLine = Files.newBufferedReader(file, Charset.defaultCharset()).readLine();
                System.out.println(firstLine);
                return FileVisitResult.CONTINUE;
            }
        }); // <- you were missing a terminating ");"
    }
}

那应该遍历目录并将每个文件的第一行打印到标准输出。我从 1.6 开始就没有接触过 Java,所以 JDK7 的东西对我来说也有点新。我认为您对什么是类和什么是接口感到困惑。在我的示例中,我们从一个名为 MyDirectoryInspector 的基本类开始,以避免混淆。给我们一个程序入口点就足够了,这是我们开始检查的主要方法。对 Files.walkFileTree 的调用需要 2 个参数,一个开始路径和一个我已内联的文件访问者。(如果您不习惯这种风格,我认为内联可能会让某些人感到困惑。)这是一种在您希望使用它的地方定义实际类的方法。您还可以单独定义 SimpleFileVisitor 并为您的调用实例化它,如下所示:

import java.nio.files.*;
public class MyDirectoryInspector extends Object 
{
    public static void main(String[] args) {
        Path startPath = Paths.get("\\CallGuidesTXT\\");
        Files.walkFileTree(startPath, new SimpleFileVisitor<Path>());
    }
}

public class SimpleFileVisitor<Path>()) { 
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                throws IOException
            {
                String firstLine = Files.newBufferedReader(file, Charset.defaultCharset()).readLine();
                System.out.println(firstLine);
                return FileVisitResult.CONTINUE;
            }
        }

如果您刚刚开始,将所有事情分开可能更有意义。在没有内联的情况下定义你的类,一步一步来,并确保你单独理解每一部分。我的第二个示例为您提供了 2 个单独的部分,一个自定义文件访问器,可用于打印它访问的每个文件的第一行,以及一个将其与 JDK Files 类一起使用的程序。现在让我们看看另一种省略语法的方法:

import java.nio.files.*;
public class MyDirectoryInspector extends Object 
{
    public static void main(String[] args) {
        Path startPath = Paths.get("\\CallGuidesTXT\\");
        Files.walkFileTree(startPath, new SimpleFileVisitor());
    }
}

public class SimpleFileVisitor()) { 
            @Override
            public FileVisitResult visitFile(Object file, BasicFileAttributes attrs)
                throws IOException
            {
                String firstLine = Files.newBufferedReader((Path)file, Charset.defaultCharset()).readLine();
                System.out.println(firstLine);
                return FileVisitResult.CONTINUE;
            }
        }

在不使用泛型的情况下,您必须将文件参数声明为 Object 类型,并在以后选择使用它时对其进行转换。通常,您不想用类定义替换接口定义或混淆两者,因为它们用于完全不同的目的。

于 2012-04-04T16:41:35.457 回答
6

Javainterface不能任何实现(即代码)——只有方法签名

尝试更改interfaceclass

public class FileVisitor<T> {
    ...
于 2012-04-04T15:55:11.493 回答
2

现在回答您修改后的帖子...

你有一个错误的地方括号:

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

之后我移动了括号new SimpleFileVisitor(startPath)以包含该visitFile方法。您在这里拥有的是一个匿名类- 您可以在其中“即时”提供实现。

于 2012-04-04T16:11:42.577 回答