Java 中的文件迭代器?

发布于 2024-10-08 05:10:40 字数 532 浏览 8 评论 0原文

你知道我在哪里可以找到一个在 Java 中实现 FileIterator (或类似的东西)的库吗?让它像下面这样工作(未经检查的代码)会很好:

Iterator it = new FileIterator();
it.addDirectory("../some-dir/");
it.addInclude("**/*.java");
it.addExclude(".svn/**");
while (it.hasNext()) {
  File f = it.next();
  System.out.println("File found: " + f);
}

Maven FileSet,但我不认为将 Maven JAR 添加到我的项目中是个好主意:)

Do you know where I can find a library that implements FileIterator (or something similar) in Java? Would be nice to have it working like the following (unchecked code):

Iterator it = new FileIterator();
it.addDirectory("../some-dir/");
it.addInclude("**/*.java");
it.addExclude(".svn/**");
while (it.hasNext()) {
  File f = it.next();
  System.out.println("File found: " + f);
}

Something similar is implemented in Maven FileSet, but I don't think it's a good idea to add Maven JAR to my project :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假面具 2024-10-15 05:10:40

查看 org.apache。 commons.io.FileUtils.iterateFiles(..)

 Iterator<File> iterateFiles(File directory,
                                          String[] extensions,
                                          boolean recursive)    

Have a look at org.apache.commons.io.FileUtils.iterateFiles(..)

 Iterator<File> iterateFiles(File directory,
                                          String[] extensions,
                                          boolean recursive)    
清眉祭 2024-10-15 05:10:40

您可以在 Commons-IO 中使用 DirectoryWalker,以下是示例:

public class FileCleaner extends DirectoryWalker {

    public FileCleaner() {
      super();
    }

    public List clean(File startDirectory) {
      List results = new ArrayList();
      walk(startDirectory, results);
      return results;
    }

    protected boolean handleDirectory(File directory, int depth, Collection results) {
      // delete svn directories and then skip
      if (".svn".equals(directory.getName())) {
        directory.delete();
        return false;
      } else {
        return true;
      }

    }

    protected void handleFile(File file, int depth, Collection results) {
      // delete file and add to list of deleted
      file.delete();
      results.add(file);
    }
  }

其他示例可以在 http://commons.apache.org/io/api-2.0/index.html?org/apache/commons/io/FileUtils.html

You can use DirectoryWalker in Commons-IO, here is example:

public class FileCleaner extends DirectoryWalker {

    public FileCleaner() {
      super();
    }

    public List clean(File startDirectory) {
      List results = new ArrayList();
      walk(startDirectory, results);
      return results;
    }

    protected boolean handleDirectory(File directory, int depth, Collection results) {
      // delete svn directories and then skip
      if (".svn".equals(directory.getName())) {
        directory.delete();
        return false;
      } else {
        return true;
      }

    }

    protected void handleFile(File file, int depth, Collection results) {
      // delete file and add to list of deleted
      file.delete();
      results.add(file);
    }
  }

Other examples can be found at http://commons.apache.org/io/api-2.0/index.html?org/apache/commons/io/FileUtils.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文