So I came accross this code to save something to a .text file using FileOutputStream
try (PrintStream out = new PrintStream(new FileOutputStream("filename.txt"))) {
out.print(text);
}
Is there any way I could pass a variable to the filname such as FileOutputStream(filename.text)?
String fileName = "filename.text"; // Your need be, I might have used .txt
try (PrintStream out = new PrintStream(new FileOutputStream(fileName))) {
out.print(text);
} catch (FileNotFoundException fnf) {
fnf.printStackTrace(); // your .text would take you here ;)
}
What you are using here is FileOutputStream's constructor which accepts a string parameter. So you can certainly modify it according to your needs.
Note - Remember to catch the exception as well