在java中选择特定类型的文件 [英] Select a particular type of file in java
本文介绍了在java中选择特定类型的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码在java中选择文件:
I am selecting files in java using the following code:
File folder = new File("path to folder");
File[] listOfFiles = folder.listFiles();
现在如果我只想选择图像文件该怎么办?
Now what to do if I want to select only image files?
推荐答案
使用 File.listFiles()
接受 FileFilter
或 FilenameFilter
定义匹配条件。
Use one of the versions of File.listFiles()
that accepts a FileFilter
or FilenameFilter
to define the matching criteria.
例如:
File[] files = folder.listFiles(
new FilenameFilter()
{
public boolean accept(final File a_directory,
final String a_name)
{
return a_name.endsWith(".jpg");
// Or could use a regular expression:
//
// return a_name.toLowerCase().matches(".*\\.(gif|jpg|png)$");
//
};
});
这篇关于在java中选择特定类型的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文