2013-08-25 2 views
0

재귀 적으로 .exe 파일을 검색하는 Java를 사용하여 메소드를 작성하려고합니다. 내가 겪고있는 문제는 "C : \ Program Files (x86) \ Google \ CrashReports"디렉토리에있을 때이다.Java 파일 사용 권한 문제

나는이 파일 때문에 항상 NullPointerException을 얻는다. 필자는 재귀 적으로 검사 할 파일 목록에 파일을 추가하지 않거나 검사 할 파일 목록에 파일을 추가하는 경우 최소한 건너 뛸 수 있도록 노력했습니다.

내 코드가 잘못 되었기 때문에 이것은 논리적 인 오류 일 수 있습니다. 자바가 어떻게이 파일을 읽을 수 있다고 생각하는지에 대한 설명을 매우 높이 평가할 것입니다.

private static List<String> exefiles = new ArrayList<String>(); 
public void findExe(File rootDir) throws SecurityException, IOException{ 
    List<File> files = new ArrayList<File>(Arrays.asList(rootDir.listFiles())); 
    List<File> directories = new ArrayList<File>(); 
    Iterator<File> iterator = files.iterator(); 
    if(files.size() > 0){ 
     while(iterator.hasNext()){ 
      File currentFile = iterator.next(); 
      if(currentFile.getName().endsWith(".exe")){ 
       exefiles.add(currentFile.getAbsolutePath()); 
      }else if(currentFile.isDirectory()){ 
       if(currentFile.canRead()){ 
        System.out.println("We can read " + currentFile.getAbsolutePath()); 
        if(currentFile.listFiles().length > 0){ 
         System.out.println(currentFile.getAbsolutePath() + " has a length greater than 0"); 
         directories.add(currentFile); 
        }else{System.out.println(currentFile.getAbsolutePath() + " does not have any files in it");} 
       }else{ 
        System.out.println("Could not add " + currentFile.getAbsolutePath() + " to directories because it could not be read"); 
       } 
      }else; 
     } 
    } 

Windows에서 파일 속성을 열면 시스템 그룹과 관리자 그룹에는 모든 권한이 있지만 사용자 그룹에는 "특수 권한"만 있습니다.

Java 7은 java.nio.file 패키지를 통해 속성을 처리하는 더 쉬운 방법을 제공하지만이 옵션은 필자의 요구에 부적합하다는 것을 알고 있습니다.

+0

변경할 수 있습니다'? 예외의 전체 스택 추적은 무엇입니까? – Jeffrey

+0

발생한 문제에 대한 일반적인 설명이 아니라 전체 스택 추적을 게시하십시오. – chrylis

답변

2

listFiles는 디렉토리를 전달하지 않거나 연결 지점과 같이 제대로 처리 할 수없는 항목을 전달하면 null을 반환합니다. 다른 패키지를 사용하면 연결 지점을보고 있는지 확인할 수 있지만 기본 파일 API로는이를 수행 할 수 없습니다. 있음을 밝혔다되고 그래서, 당신은 대신 무효에 대한 빠른 검사를 수행 그래서 왜 당신이 '다른 포함한

}else if(currentFile.isDirectory()){ 

}else if(currentFile.isDirectory() && currentFile.listFiles()!=null){ 
+1

당신은 학자이고 신사입니다. 고맙습니다. – davelupt

+0

나는 그렇게 생각한다. 천만에요. – chiliNUT