본문 바로가기

Program/Java Core

[Java] 재귀함수를 이용한 폴더 삭제

public void deleteDirectory(String pDirPath) {

File sourceDir = new File(pDirPath);

String[] sourceDirFiles = sourceDir.list();


if (sourceDir.isDirectory()) {

for (int i = 0; i < sourceDirFiles.length; i++) {

File file = new File(pDirPath + File.separatorChar + sourceDirFiles[i]);


if (file.isDirectory()) {

deleteDirectory(pDirPath + File.separatorChar + file.getName());

} else {

file.delete();

}

}

}


if (sourceDir.isDirectory()) {

sourceDir.delete();

}

}