使用 Java 查找目录中的第一个文件

发布于 2024-11-30 16:53:41 字数 213 浏览 2 评论 0原文

我有某种批处理程序,应该从目录中获取文件并处理它。

由于该程序应该:

  • 在 JDK 5 上运行、
  • 小(无外部库)
  • 且快! (一声巨响)

...只从目录中选择一个文件的最佳方法是什么 - 不使用 File.list() (可能是数百个文件) ?

I have some sort of batch program that should pick up a file from a directory and process it.

Since this program is supposed to:

  • run on JDK 5,
  • be small (no external libraries)
  • and fast! (with a bang)

...what is the best way to only pick one file from the directory - without using File.list() (might be hundreds of files)?

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

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

发布评论

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

评论(6

天涯离梦残月幽梦 2024-12-07 16:53:41

在 Java 7 中,您可以使用 DirectoryStream,但在 Java 中5、获取目录条目的唯一方法是list()listFiles()

请注意,列出包含数百个文件的目录并不理想,但与处理其中一个文件相比,仍然可能没什么大不了的。但一旦目录包含数千个文件,它可能就会开始出现问题。

In Java 7 you could use a DirectoryStream, but in Java 5, the only ways to get directory entries are list() and listFiles().

Note that listing a directory with hundreds of files is not ideal but still probably no big deal compared to processing one of the files. But it would probably start to be problematic once the directory contains many thousands of files.

情绪失控 2024-12-07 16:53:41

使用编写为仅接受一次的 FileFilter (或 FilenameFilter),例如:

File dir = new File("/some/dir");
File[] files = dir.listFiles(new FileFilter() {
    boolean first = true;
    public boolean accept(final File pathname) {
        if (first) {
            first = false;
            return true;
        }
        return false;
    }
});

Use a FileFilter (or FilenameFilter) written to accept only once, for example:

File dir = new File("/some/dir");
File[] files = dir.listFiles(new FileFilter() {
    boolean first = true;
    public boolean accept(final File pathname) {
        if (first) {
            first = false;
            return true;
        }
        return false;
    }
});
廻憶裏菂餘溫 2024-12-07 16:53:41

从您所说的看来,您希望一次处理目录中的每个文件(包括添加到目录中的文件)。您可以执行以下操作:在添加文件时生成通知的目录上设置监视器。然后,您可以处理收到通知的每个文件。由于您使用 JDK 5 ,我建议使用 jpathwatch 。请注意,在尝试处理文件之前,您需要确保文件写入已完成。启动监视器以确保您将处理每个新文件后,请一次性使用文件列表来处理当前内容。

It seems from what you said that you want to process every file in the directory once (including files that get added to the directory). You can do the following: set a monitor on the directory that generates notifications when files are added. you then process each file that you get notified about. Since you use JDK 5 , i suggest using jpathwatch . note that you need to make sure the file writing has finished before trying to process it. after starting the monitor to insure you will be processing every new file, make a one time usage of file listing to process the current content.

旧情勿念 2024-12-07 16:53:41

编辑:我的实现使用了 .list() 正如你所说,它不会,但它可能仍然具有一些价值:)

如果你看看 File 实现 public String[] list() 方法似乎比 File[ 的开销更少] 列表文件()。所以最快应该是

String[] ss = myDir.list();
File toProcess = null;
for(int i = o ; i< ss.length ; i++){
 toProcess = new File(myDir.list()[i], myDir));
 if(toProcess.isFile())break;
}

From File.class

public File[] listFiles() {
String[] ss = list();
if (ss == null) return null;
int n = ss.length;
File[] fs = new File[n];
for (int i = 0; i < n; i++) {
    fs[i] = new File(ss[i], this);
}
return fs;
}

Edit: My implementation made use of .list() as you said it wouldn't but It may hold some value anyways :)

If you look at the File implementation public String[] list() method seems to have less overhead than File[] listFiles(). So fastest should be

String[] ss = myDir.list();
File toProcess = null;
for(int i = o ; i< ss.length ; i++){
 toProcess = new File(myDir.list()[i], myDir));
 if(toProcess.isFile())break;
}

From File.class

public File[] listFiles() {
String[] ss = list();
if (ss == null) return null;
int n = ss.length;
File[] fs = new File[n];
for (int i = 0; i < n; i++) {
    fs[i] = new File(ss[i], this);
}
return fs;
}
已下线请稍等 2024-12-07 16:53:41

如果看一下 FileSystem 类(它归结为用于文件系统访问),则只有 list 方法,因此在“纯”JAVA 中似乎没有其他方法来选择文件,而是将它们全部列在 String 数组中。

If one look at the class class FileSystem which it boils down to for filesystem access there is only the list method so there seems to be no other way in "pure" JAVA to select a file than to list them all in a String array.

红衣飘飘貌似仙 2024-12-07 16:53:41

Java 1.5 上没有好的解决方案,您可以使用过滤器仅获取 1 个文件,但 java 将只返回一个文件,但无论如何都会解析所有文件。如果您不需要实际的文件对象,您可以尝试类似 Runtime.getRuntime().exec("dir") 将返回的字符串拆分为 \n 并打印输出第一行:-P

There is no good solution here on Java 1.5, you can use a filter to get only 1 file, but then java will only return one file but parse over all of them anyways. If you don't need the actual file object you could try something like Runtime.getRuntime().exec("dir") split the returned string on \n and print out the first line :-P

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