2013-03-06 2 views
1

나는 그 다음 문제가 있습니다. 내 응용 프로그램은 파일 목록을 읽고 사용자에게 이름을 보여줘야합니다. 앱을 처음 시작하면 앱의 특정 폴더를 만들어야합니까? 그러면 비어 있는지 어떻게 확인할 수 있습니까?폴더가 비어 있는지 확인하십시오 (내부 저장소)

// check if appfolder is created and if not through an 
    // exception 
    File path = new File("yourDir"); 
    if (!path.exists()) { 
     if (!path.mkdirs()) { 
      try { 
       throw new IOException(
         "Application folder can not be created. Please check if your memory is writable and healthy and then try again."); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return false; 
     } 
    } else { 
     Log.i("app", "directory is: " + path.getPath()); 
    } 

exists() 검사를 경로가 존재 여부와 path.exists()은 단순히 당신을 위해 하나를 만드는 경우 : 오류가 하나를 만드는 경우 하나를 생성하고 경고하지 않을 경우

답변

4

당신이 내부 저장소의 디렉토리를 만들어 가정하면, 이것은 그냥 빨리 샘플 코드이

File dir = context.getDir("somePath", Context.MODE_PRIVATE); 
File[] children = dir.listFiles(); 
if(children.length > 0) 
{ 
    // the directory is not empty 
} 
else 
{ 
    // the directory is empty. 
} 

같은 디렉토리에 자식 개체의 수를 얻을 수 있습니다. 더 좋은 방법은 사용자 지정 FileFilterdir.listFiles()과 함께 사용하여 자체 및 상위 별칭을 제외하는 것입니다. 항상 결과 목록에서 제외 될지 잘 모르겠습니다.

2

다음 코드는 폴더가 이미 존재하는 경우 확인합니다.

관련 문제