How to go through folders and then write every file within those folders? If there is a folder within a folder we need to go through that first and then get back to the older directory.
Asked
Active
Viewed 101 times
-1
-
What do you mean with "write every file"? What have you tried? – ddmps Mar 30 '13 at 16:14
-
6Try with a recursive method that uses [`File#listFiles`](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles()) and [`File#isDirectory`](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#isDirectory()). Come back when you have a real question. – Luiggi Mendoza Mar 30 '13 at 16:16
-
possible duplicate of [Recursively list files in Java](http://stackoverflow.com/questions/2056221/recursively-list-files-in-java) – Rob Hruska Mar 30 '13 at 18:08
2 Answers
1
You just need to call a recursive function:
public void writeStuff(final File file) {
if(file.isDirectory()) {
for(final File childFile : file.listFiles()) {
writeStuff(childFile);
}
} else {
//do stuff with file
}
}

Boris the Spider
- 59,842
- 6
- 106
- 166
-
-
-
This is already covered in an answer on possible duplicate: [Recursively list files in Java](http://stackoverflow.com/questions/2056221/recursively-list-files-in-java) – Luiggi Mendoza Mar 30 '13 at 16:28
0
Oracle provides a tutorial based on the FileVisitor interface that explains how one can go by.

matsev
- 32,104
- 16
- 121
- 156