System.out.println to text file

后端 未结 3 551
南旧
南旧 2020-12-21 04:06

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: \" + Syst         


        
相关标签:
3条回答
  • 2020-12-21 04:34

    You can do,

    PrintStream fileStream = new PrintStream("filename.txt");
    System.setOut(fileStream);
    

    Then any println statement will go into the file.

    0 讨论(0)
  • 2020-12-21 04:36

    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);
    }
    
    0 讨论(0)
  • 2020-12-21 04:41

    You are using standard System output stream which writes to console.You need to use PrintStream class,create a file and write to it.Example is below :

    How to write console output to a txt file

    0 讨论(0)
提交回复
热议问题