So the following code below is to search for a file in a directory. It works fine until I want to test if a file doesn't exist in the directory. I get no output message when I want it to say "Either dir does not exist or is not a directory". Not sure what I am doing wrong.
class FileSearcher{
public static void main(String[] args) {
File dir = new File("C:");
FilenameFilter filter = new FilenameFilter() {
public boolean accept
(File dir, String name) {
//File Name to be retrieved starts with
return name.startsWith("z");
}
};
String[] file = dir.list(filter);
if (file == null) {
//Prints out message if file not in directory
System.out.println("Either dir does not exist or is not a directory");
}
else {
for (int i=0; i< file.length; i++) {
String filename = file[i];
System.out.println(filename);
}
}
}
}