使用FilenameFilter [英] Use of FilenameFilter
问题描述
我有一个目录:
文件dir = new File(MY_PATH);
我想列出所有名称以整数字符串表示的文件, 10,20。我知道我应该使用:
dir.list(FilenameFilter filter);
如何定义我的 FilenameFilter
?
P。我的意思是文件名可以是任何整数字符串,例如10或2000000或3452345。只要文件名是一个整数字符串就没有数字限制。
您应该覆盖 FilenameFilter
并确保参数 name
只有数字字符。你可以使用 :
@Override
public boolean accept(File dir,String name){
return name.matches([0-9] +);
}
});
I have a directory:
File dir = new File(MY_PATH);
I would like to list all the files whose name is indicated as integer numbers strings, e.g. "10", "20".
I know I should use:
dir.list(FilenameFilter filter);
How to define my FilenameFilter
?
P.S. I mean the file name could be any integer string, e.g. "10" or "2000000" or "3452345". No restriction in the number of digits as long as the file name is a integer string.
You should override accept
in the interface FilenameFilter
and make sure that the parameter name
has only numeric chars. You can check this by using matches
:
String[] list = dir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.matches("[0-9]+");
}
});
这篇关于使用FilenameFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!