Writing to console and text file

后端 未结 3 1321
闹比i
闹比i 2020-11-29 08:44

I found the code below from the internet, works but it doesn\'t write the printed console to omt.txt, it only writes the System.out.println statements after the

3条回答
  •  盖世英雄少女心
    2020-11-29 09:35

    The reason is :

    The java.lang.System.setOut() method reassigns the "standard" output stream.

    so when you use System.out.println it will print only in the text file

    So , if you want to print on the text file and on the console , Try this :

       

        FileOutputStream fos = new FileOutputStream(f);
        PrintStream ps = new PrintStream(fos);
        ps.println("THIS is what I see on the text file, but not on CONSOLE");
        System.out.println("THIS is what I see on the text file, but not on CONSOLE");
    
            for (int i = 0; i < 4; i++) {
    
                ps.println("Testing");
                System.out.println("Testing");
            } 
    

提交回复
热议问题