标签:public 多少 class use 相关 workspace print tor 面向对象
package com.study;
/**
* 用面向对象方法实现打印文件夹目录、输出每个文件和文件夹大小
* 计算此文件夹下共有多少个子文件夹和文件
*/
import java.io.File;
public class DirLength {
private long len;
private String path;
private File src;
public int getFileLength() {
return fileLength;
}
public int getDirLength() {
return dirLength;
}
private int fileLength = 0;
private int dirLength = 0;
public DirLength(String path){
this.path = path;
this.src = new File(path);
}
private void testFile(File src,int length) {
if (src.isDirectory()) {
for (int i = 0; i < length; i++) {
System.out.print("-");
}
System.out.print(src.getName()+": ");
count(src);
System.out.println();
for(File s:src.listFiles()) {
testFile(s, length+1);
}
}else if (src.isFile()) {
for (int i = 0; i < length; i++) {
System.out.print("-");
}
System.out.print(src.getName()+": ");
count(src);
System.out.println();
}else {
return;
}
}
private void count(File src) {
long cou = 0;
if (src.isFile()) {
cou = cou+src.length();
this.fileLength++;
}else if (src.isDirectory()) {
for(File s :src.listFiles()) {
count(s);
this.dirLength++;
}
}else {
return;
}
System.out.print(cou);
}
public static void main(String[] args) {
DirLength aaa = new DirLength("C:\\Users\\Administrator\\eclipse-workspace");
aaa.testFile(aaa.src,0);
System.out.printf("此文件夹包含%d个子文件夹,%d个文件",aaa.getDirLength(),aaa.getFileLength() );
}
}
标签:public 多少 class use 相关 workspace print tor 面向对象
原文地址:https://www.cnblogs.com/LuJunlong/p/11663687.html