67 279
Assignments Done
99,1%
Successfully Done
In November 2018

Answer to Question #63986 in Java | JSP | JSF for hamod

Question #63986
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
Expert's answer
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!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions