65 859
Assignments Done
99,1%
Successfully Done
In October 2018

Answer to Question #4864 in Java | JSP | JSF for Anthony

Question #4864
Hi there

im trying to check if a directory contains a spesific file extention. example if the directory ( listOfFiles) contains a pdf file.

The problem im having is the checking part. Both my if and else statement execute when the directory contains the file.

If someone can help me with that peace of code please .
Expert's answer
import java.io.File;

// This example searches for .java files in /src/
dir of the project
public class Test {

public static void
main(String[] args) {
String path = "src";
File dir = new
File(path);
if (dir.isDirectory()) {
File[] listOfFiles =
dir.listFiles();
for (File file : listOfFiles)
if
(file.getName().endsWith("java"))
System.out.println(file.getAbsolutePath());
}
}
}

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