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());
}
}
}
LATEST TUTORIALS
APPROVED BY CLIENTS