I have this code where I recursively list all the files from a folder. My question is how to print the output from the console to a .txt file? It would help me a lot if someone could add what is needed in my code.
import java.io.*;
public class Exp {
public static void main(String[] args) throws IOException {
File f = new File("C:\\Users\\User\\Downloads");
rec(f);
}
public static void rec(File file) throws FileNotFoundException {
File[] list = file.listFiles();
for (File f : list) {
if (f.isFile()) {
System.out.println(f.getName());
}
else if (f.isDirectory()) {
rec(f);
}
}
}
}