I'd like to do a search for folders/directories from java, and go into those folders/directories in java. I guess it's called system utilities? Any tutorials out there, or books on the subject?
Thanks ;)
I use this code to get all ZIP files in a folder. Call this recursively checking for the file object to be a sub directory again and again.
public List<String> getFiles(String folder) {
List<String> list = new ArrayList<String>();
File dir = new File(folder);
if(dir.isDirectory()) {
FileFilter filter = new FileFilter() {
public boolean accept(File file) {
boolean flag = false;
if(file.isFile() && !file.isDirectory()) {
String filename = file.getName();
if(!filename.endsWith(".zip")) {
return true;
}
return false;
}
};
File[] fileNames = dir.listFiles(filter);
for (File file : fileNames) {
list.add(file.getName());
}
return list;
}
You could use Apache Commons FileUtils (see: http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html) and specifically the listFiles method there, which can do this recursively and use filters (so it saves you the writing of the recursion yourself and answers the search you mentioned).
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