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 .
1
Expert's answer
2012-05-18T11:54:10-0400
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 the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS