使用 Java 查找目录中的第一个文件
我有某种批处理程序,应该从目录中获取文件并处理它。
由于该程序应该:
- 在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 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()
andlistFiles()
.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.
使用编写为仅接受一次的
FileFilter
(或FilenameFilter
),例如:Use a
FileFilter
(orFilenameFilter
) written to accept only once, for example:从您所说的看来,您希望一次处理目录中的每个文件(包括添加到目录中的文件)。您可以执行以下操作:在添加文件时生成通知的目录上设置监视器。然后,您可以处理收到通知的每个文件。由于您使用 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.
编辑:我的实现使用了 .list() 正如你所说,它不会,但它可能仍然具有一些价值:)
如果你看看 File 实现 public String[] list() 方法似乎比 File[ 的开销更少] 列表文件()。所以最快应该是
From File.class
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
From File.class
如果看一下 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.
Java 1.5 上没有好的解决方案,您可以使用过滤器仅获取 1 个文件,但 java 将只返回一个文件,但无论如何都会解析所有文件。如果您不需要实际的文件对象,您可以尝试类似
Runtime.getRuntime().exec("dir")
将返回的字符串拆分为\n
并打印输出第一行:-PThere 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