import java.io.File;
public class ex9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f = new File("c:/temp");
File[] subfiles = f.listFiles();
int cnt = 0;
System.out.println(f.getPath() + "디렉터리의 .txt 파일을 모두 삭제합니다....");
for(int i=0;i<subfiles.length;i++) {
String s = subfiles[i].getName();
int index = s.lastIndexOf(".txt");
if(index != -1) {
System.out.println(subfiles[i].getPath()+" 삭제");
subfiles[i].delete();
cnt++;
}
}
System.out.println("총 " + cnt +"개의 .txt 파일을 삭제하였습니다.");
}
}
결과
c:\temp디렉터리의 .txt 파일을 모두 삭제합니다....
c:\temp\appended.txt 삭제
c:\temp\elvis1-복사본.txt 삭제
c:\temp\elvis1.txt 삭제
c:\temp\elvis2.txt 삭제
c:\temp\phone.txt 삭제
c:\temp\system.txt 삭제
c:\temp\test.txt 삭제
c:\temp\test2.txt 삭제
총 8개의 .txt 파일을 삭제하였습니다.
'명품JAVA프로그래밍 > 8장 입출력 스트림과 파일 입출력' 카테고리의 다른 글
[명품JAVA프로그래밍] 8장 실습문제 10번 (0) | 2022.02.25 |
---|---|
[명품JAVA프로그래밍] 8장 실습문제 8번 (0) | 2022.02.24 |
[명품JAVA프로그래밍] 8장 실습문제 6번 (0) | 2022.02.24 |
[명품JAVA프로그래밍] 8장 실습문제 5번 (0) | 2022.02.24 |
[명품JAVA프로그래밍] 8장 실습문제 4번 (0) | 2022.02.24 |