Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problems getting output messsage when testing program

$
0
0
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);
        		}
      		} 
   	}
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>