write a java code segment that will return the number of file found inside the directory "d:/windows" iff the named directory does indeed exists
1
Expert's answer
2016-11-01T16:05:07-0400
public class FileCounter {
/** * Returns the number of files found inside the directory "d:/windows" if * the named directory does indeed exists or -1 otherwise * @return */ public static int countFiles() { java.io.File winDir = new java.io.File("d:/windows"); if (winDir.exists()) return winDir.listFiles().length; else return -1; }
/** * Driver method. Prints out the number of files in d:/windows. * @param args */ public static void main(String[] args) { int count = countFiles(); if (count > -1) System.out.println("Number of files in d:/windows: " + count); else System.out.println("No such directory exists"); } }
Comments
Leave a comment