3 回答

TA貢獻(xiàn)1891條經(jīng)驗(yàn) 獲得超3個(gè)贊
嘗試這個(gè):
@Override
public boolean accept(File dir, String name) {
File file = new File(dir, name);
return name.endsWith(x) && file.length() == size;
}

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊
@Override
public boolean accept(File dir, String name) {
//i can't use the dir.length because it checks the size of the directory and not the inside files
if(name.endsWith(x))
{
File f = new File(dir.getPath(), name);
return f.length() == size;
}
return false;
}

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用 FileFiler 而不是 FilenameFilter 更適合您打算實(shí)現(xiàn)的目標(biāo)。您的過濾器類將如下所示:
import java.io.File;
import java.io.FileFilter;
public class MyFileFilter implements FileFilter {
private String x;
private int size;
public MyFileFilter(String x, int size){
this.x = x;
this.size = size;
}
@Override
public boolean accept(File file) {
return file.endsWith(x) && file.length() == size;
}
}
添加回答
舉報(bào)