Saturday, 24 August 2013

Java File Permission Problems

Java File Permission Problems

I'm trying to write a method using Java which will recursively search for
.exe files. The problem I'm having is when it comes to the directory of
"C:\Program Files (x86)\Google\CrashReports"
It seems like no matter what I attempt I always get a NullPointerException
due to this file. I've tried making sure that I either don't add it to the
list of files to be recursively checked, or at least to skip over it if it
does make it into the list of files to be examined.
As it stands my code is incorrect, however this may very well be a logical
fallacy rather, I would greatly appreciate an explanation as to how Java
thinks it can read this file.
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;
}
}
When I open the files properties with Windows, the System and
Administrator groups have full control, while the Users group only has
"special permissions."
I know that Java 7 provides easier ways to deal with attributes via the
java.nio.file package, but this option is unsuitable for my needs.

No comments:

Post a Comment