Answer to Question #63024 in Java | JSP | JSF for jonney

Question #63024
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");
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS