Getting the filenames of all files in a folder
To get the filenames of all files in a folder in Java, you can use the File
class and its list
method.
Here's an example:
import java.io.File;
File folder = new File("/path/to/folder");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
System.out.println(file.getName());
}
}
This will print the filenames of all files in the specified folder. If you want to include subfolders, you can use the listFiles
method with a FileFilter
to recursively list all files in the folder and its subfolders.
Here's an example:
import java.io.File;
File folder = new File("/path/to/folder");
File[] listOfFiles = folder.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isFile();
}
});
for (File file : listOfFiles) {
System.out.println(file.getName());
}
This will print the filenames of all files in the specified folder and its subfolders.