Web/JAVA
[Java] 특정 폴더에 특정 파일 삭제
oSsonGo
2022. 3. 16. 22:14
- 특정 문자가 포함된 파일 삭제
String dirPath = "/home/oSsonGo";
File dirFile = new File(dirPath);
String fileList[] = dirFile.list();
for(int i = 0; i < fileList.length; i++) {
String chkFileNm = fileList[i];
if(chkFileNm.contains("test")) {
File delFile = new File(dirPath + File.separator + chkFileNm);
delFile.delete();
}
}
- 특정문자로 시작하는 파일 삭제
String dirPath = "/home/oSsonGo";
File dirFile = new File(dirPath);
String fileList[] = dirFile.list();
for(int i = 0; i < fileList.length; i++) {
String chkFileNm = fileList[i];
if(chkFileNm.startsWith("test_")) {
File delFile = new File(dirPath + File.separator + chkFileNm);
delFile.delete();
}
}