I was searching a way to get System.out.println
texts and save them on a .txt file, for example:
System.out.println("Java vendor: " + System.getProperty("java.vendor"));
System.out.println("Operating System architecture: " + System.getProperty("os.arch"));
System.out.println("Java version: " + System.getProperty("java.version"));
System.out.println("Operating System: " + System.getProperty("os.name"));
System.out.println("Operating System Version: " + System.getProperty("os.version"));
System.out.println("Java Directory: " + System.getProperty("java.home"));
I want a .txt file to the output, any ideas? Thank you
You can do,
PrintStream fileStream = new PrintStream("filename.txt");
System.setOut(fileStream);
Then any println statement will go into the file.
First you need to declare a String text
that contains your message you want to output:
String text = "Java vendor: " + System.getProperty("java.vendor");
Then you can use try-with-resources statement (since JDK 7) which will automatically close your PrintWriter
, when all the output done:
try(PrintWriter out = new PrintWriter("texts.txt") ){
out.println(text);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With