반응형
문제
C:\temp에 있는 .txt 파일만 삭제하는 프로그램을 작성하라. C:\나 C:\wingdows 등의 디렉터리에 적용하면 중요한 파일이 삭제될 수 있으니 조심하라.
출력 예시
C:\temp디렉터리의 .txt 파일의 모두 삭제합니다....
C:\temp\p.txt 삭제
C:\temp\phone.txt 삭제
C:\temp\test.txt 삭제
총 3개의 .txt 파일을 삭제하였습니다.
소스 코드
import java.io.*;
public class Main9 {
public static void DeleteFile(File temp) {
File[] subFiles = temp.listFiles();
int count=0;
System.out.println("C:\\temp디렉터리의 .txt 파일의 모두 삭제합니다....");
for(int i = 0;i<subFiles.length;i++)
{
String name = subFiles[i].getName();
int index = name.lastIndexOf(".txt");
if(index != -1)
{
subFiles[i].delete();
count++;
System.out.println("총 "+count+"개의 .txt파일을 삭제하였습니다.");
}
}
}
public static void main(String[] args) {
File f = new File("파일경로");
DeleteFile(f);
}
}
반응형
'명품 JAVA 프로그래밍' 카테고리의 다른 글
명품 JAVA 프로그래밍 9장 실습문제 1 (0) | 2021.12.11 |
---|---|
명품 JAVA 프로그래밍 7장 실습문제 12 (0) | 2021.12.09 |
명품 JAVA 프로그래밍 8장 실습문제 8 (0) | 2021.12.08 |
명품 JAVA 프로그래밍 8장 실습문제 7 (0) | 2021.12.08 |
명품 JAVA 프로그래밍 8장 실습문제 6 (0) | 2021.12.08 |