Answer to Question #63986 in Java | JSP | JSF for hamod
write a java code segment that will return the number of files founds inside the directory "d:/windows" iff the named directory does indeed exists
1
2016-12-13T11:09:14-0500
public class Main {
public static void main(String[] args) throws IOException {
System.out.println(getFilesCount(Paths.get("d:/windows")));
}
public static int getFilesCount(Path dir) throws IOException {
int countFiles = 0;
if(Files.isDirectory(dir)) {
try(DirectoryStream<Path> files = Files.newDirectoryStream(dir)) {
for(Path file : files) {
if(Files.isRegularFile(file) || Files.isSymbolicLink(file)) {
countFiles++;
}
}
}
}
else
throw new NotDirectoryException(dir + " is not directory");
return countFiles;
}
}
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!
Learn more about our help with Assignments:
JavaJSPJSF
Comments
Leave a comment