import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileDirectoryEx01 {
static int dirCount = 0;
static int fileCount = 0;
public static void main(String[] args) throws IOException {
Long startTime = System.currentTimeMillis();
dirSearch("C:\\spring");
System.out.println("디렉토리 수 : "+ dirCount);
System.out.println("파일 수 : "+ fileCount);
System.out.println("소요시간 : "+ ((System.currentTimeMillis()-startTime)/1000));
}
public static void dirSearch(String dirName) throws MalformedURLException {
File file = new File(dirName);
for(File f:file.listFiles()) {
formatter(f);
if(f.isDirectory()) {
dirSearch(f.toString());
}
}
}
public static void formatter(File file) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
StringBuilder sb = new StringBuilder(100);
sb.append(df.format(new Date(file.lastModified())));
if(file.isDirectory()) {
sb.append(" [DIR] ");
dirCount++;
}else {
fileCount++;
sb.append(file.canRead() ? " [R": "[ ");
sb.append(file.canWrite() ? "W" : " ");
sb.append(file.canExecute() ? "E]" : " ]");
}
System.out.printf("%s %6s %s %n", sb.toString(), file.length(), file.toString() );
}
}
핵심 키워드
- 재귀
- StringBuilder 내부 버퍼
- 날짜 형식화