How would I read all files in a directory?
For example, I would have a bunch of images in a folder and would want to load them in.
Or would have a bunch of levels as .txt files and would load them?
This code is based on an exerp of a library I am developing:
ArrayList<File> files=new ArrayList<File>();
void visitFile(File f) {
try {
if (f.isDirectory()) {
final File[] children=f.listFiles();
if (children!=null) {
for (File child : children) visitFile(child);
}
} else files.add(f);
}
catch(Exception e) {
}
}
This code takes a file Object and checks if it is a directory. If that is the case it will check the contents of the folder.
You can easily create a File Object by invoking it’s constructor using the absolute path of the directory. You can get it from a relative Path using sketchPath(String)
.
This doc might be useful to you:
File (Java Platform SE 7 ) (oracle.com)
1 Like