【发布时间】:2013-08-27 19:10:07
【问题描述】:
我正在尝试使用 Java 编写一个递归搜索 .exe 文件的方法。我遇到的问题是“C:\Program Files (x86)\Google\CrashReports”的目录
似乎无论我尝试什么,由于此文件,我总是会收到 NullPointerException。我已经尝试确保我要么不将它添加到要递归检查的文件列表中,要么至少跳过它(如果它确实进入要检查的文件列表)。
就目前而言,我的代码是不正确的,但这很可能是一个逻辑谬误,我非常感谢 Java 认为它可以读取该文件的方式的解释。
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 包提供了更简单的方法来处理属性,但是这个选项不适合我的需要。
【问题讨论】:
-
为什么要包含
else;?异常的完整堆栈跟踪是什么? -
请发布整个堆栈跟踪,而不仅仅是您遇到的问题的一般描述。
标签: java recursion file-permissions