In my program I'm trying to print my output to a file but the file is empty. The program reads text files in a directory. I get the desired output to the console but not in the output.text file. Not sure what is missing in the program. thanks in advance for the help.
import java.io.*;
public class CacheData {
public static void main(String[] args) throws IOException {
String target_dir = "C:\\Files";
String output = "C:\\Files\\output.txt";
File dir = new File(target_dir);
File[] files = dir.listFiles();
for (File textfiles : files) {
if (textfiles.isFile() && textfiles.getName().endsWith(".txt")) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(textfiles));
String line;
while ((line = inputStream.readLine()) != null) {
System.out.println(line);
PrintWriter outputStream = new PrintWriter(output);
outputStream.println();
outputStream.close();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}
}
}