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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7104898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:38:19  来源:igfitidea点击:

Find first file in directory with Java

javaiojava-io

提问by stephanos

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)
  • 在 JDK 5 上运行,
  • 小(没有外部库)
  • 快!(砰的一声)

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

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

回答by Michael Borgwardt

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

在 Java 7 中,您可以使用DirectoryStream,但在 Java 5 中,获取目录条目的唯一方法是list()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.

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

回答by stephanos

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.

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

回答by Alistair A. Israel

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

使用 a FileFilter(or 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;
    }
});

回答by Farmor

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

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

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

如果您查看 File 实现 public String[] list() 方法似乎比 File[] listFiles() 方法的开销更少。所以最快的应该是

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

从 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;
}

回答by Farmor

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.

如果查看它归结为文件系统访问的类 FileSystem ,则只有 list 方法,因此在“纯”JAVA中似乎没有其他方法可以选择文件而不是将它们全部列出在字符串数组中。

回答by Ali

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 \nand print out the first line :-P

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