안드로이드 개발 질문/답변
(글 수 45,052)
/sdcard/download/ 폴더 내의 다운받은 파일을 삭제하려했습니다.
File dir = new File("/sdcard/download/");
File[] children = dir.listFiles();
for(int i=0;i<children.length;i++)
{
if((!children[i].isDirectory()) && children[i].getName().startsWith(prefix))
{
children[i].delete();
}
}
위처럼 코드를 짜서 실행을 했으나 delete() 함수가 계속 false를 리턴하고 있습니다.
퍼미션이나 exception 처리는 다 했고, 잘 못된 부분은 없어 보이는데 삭제 성공을 하지못하고있습니다.
삭제를 완료하려면 어떻게 해야할까요?
2010.06.30 17:51:25
(추천:
1 / 0)
디렉토리내 파일이 남겨져 있으면 디렉토리가 지워지지 않습니다. 따라서 디렉토리 삭제시 디렉토리 안에 내용부터 지우고 디렉토리를 삭제해야 올바르게 지워집니다.
/**
* File Delete (Recursive 메소드로 구현 -> fname 까지 제거)
* @param fname File Path
* @return
*/
public boolean delete(String fname){
File f = new File(fname);
String[] list = null;
String path = fname;
if(f.isDirectory()){
list = f.list();
for(int i=0; i<list.length; i++){
delete(path + "/" + list[i]);
}
}
if(!f.exists()){
Log.i(TAG , fname+" is not Found~!!");
return false;
}
return f.delete();
}




혹시 읽기전용?