有一个目录,计算该目录下所有文件名包含"Dir"文件的大小总和
直接上代码
package practices;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
public class Test3 {
private File[] files;
public void initFile(File file,String name){
files = file.listFiles(new FileFilter(){
@Override
public boolean accept(File pathname) {
return pathname.getPath().indexOf(name) != -1;
}
});
}
public int fileTotalSize(){
int i = 0;
for (File file2 : files) {
i += file2.length();
}
return i;
}
public static void main(String[] args) {
Test3 test3 = new Test3();
test3.initFile(new File("F:\\think_in_java_git\\java_io_system\\src\\practices"), "Dir");
for(File file : test3.files)
System.out.println(file);
int fileTotalSize = test3.fileTotalSize();
System.out.println("====result====");
System.out.println(fileTotalSize);
}
}
结果