package io;
import lombok.Getter;
import lombok.Setter;
import java.io.File;
@Setter
@Getter
public class StudyDir {
private long dirSize = 0;
private int fileCount = 0;
private int dirCount = 0;
public static void main(String[] args) {
String path = "D:/Code/IDEAWorkspace/ProjectMaven/SXTTest";
File file = new File(path);
StudyDir studyDir = new StudyDir();
//打印文档结构树
studyDir.printDir(new File(path), 0);
//统计路径下文件的大小、文件夹的个数、文件的个数
studyDir.dirTest(file);
System.out.println(studyDir.getDirSize());
System.out.println(studyDir.getDirCount());
System.out.println(studyDir.getFileCount());
}
private void dirTest(File file) {
//判断是否为文件
if (file.isFile()) {
this.dirSize += file.length();
this.fileCount++;
}
//判断是否为文件夹
if (file.isDirectory()) {
this.dirCount++;
File[] list = file.listFiles();
for (File f : list) {
dirTest(f);
}
}
}
private void printDir(File file, int level) {
//打印层次划分
for (int i = 0; i < level; i++) {
System.out.print("-->");
}
//打印文件名
System.out.println(file.getName());
if (file.isDirectory()) {
File[] list = file.listFiles();
for (File f : list) {
printDir(f, level + 1);
}
}
}
}