我有一些没有扩展名的文件uuid
String uuid = "507f23e3-7634-42b6-8bef-df3eb87a595b"
如何查找具有此 uuid 和自定义扩展名的文件
最佳答案
这个怎么样:
public static void main(String[] args)
{
JFileChooser c = new JFileChooser();
File dir = new File("directoryOfFile");
final String uuid = "507f23e3-7634-42b6-8bef-df3eb87a595b";
dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name)
{
// checks for a filename consisting of uuid with a custom extension
return name.startsWith(uuid + ".");
}
});
}
这当然也会匹配看起来像这样的文件:
507f23e3-7634-42b6-8bef-df3eb87a595b.abc.def
在这种情况下,“abc”不是扩展的一部分,因此,为了避免这种情况(如果您愿意),您可以将 startsWith
行替换为:
return name.startsWith(uuid + ".") && name.lastIndexOf(".") == uuid.length();
上面只是检查最后一个点是否在 uuid
的末尾。
关于java - 如何查找没有扩展名的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17741170/