There is a folder: C:\\Users\..myfolder
It contains .pdf
files (or any other, say .csv). I cannot change the names of those files, and I do not know the number of those files. I need to loop all of the files one by one.
How can I do this?
(I know how to do this if I knew the names)
Just use File.listFiles
final File file = new File("whatever");
for(final File child : file.listFiles()) {
//do stuff
}
You can use the FileNameExtensionFilter
to filter your files too
final FileNameExtensionFilter extensionFilter = new FileNameExtensionFilter("N/A", "pdf", "csv"//, whatever other extensions you want);
final File file = new File("whatever");
for (final File child : file.listFiles()) {
if(extensionFilter.accept(child)) {
//do stuff
}
}
Annoyingly FileNameExtensionFilter
comes from the javax.swing
package so cannot be used directly in the listFiles()
api, it is still more convenient than implementing a file extension filter yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With