写入控制台和文本文件 [英] Writing to console and text file

查看:187
本文介绍了写入控制台和文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上找到下面的代码,但是它不会将打印的控制台写入omt.txt,它只会写入 System.out.println 语句在第二个catch块之后。如果你运行代码,你会明白我的意思。我想要的是写在控制台上的omt.txt文件是所有...



经过一些答案,我看到我的问题不清楚,对不起。
我想将控制台输出保存到omt.txt文本文件。如果在控制台上打印Hello 123,它也应该在omt.txt文件中。换句话说,控制台上打印的任何内容都应该同时写在om.txt文件中,或者可以在控制台执行后,但应该1对1同样!

  import java.io.File; 
import java.io.FileOutputStream;
import java.io.PrintStream;

public class Wrt_file {

public static void main(String [] args){
System.out.println(这是我在控制台上看到的。但不是TEXT文件);

文件f = new File(omt.txt);
if(!f.exists())
{
try {
f.createNewFile();
} catch(Exception e){
e.printStackTrace();
}
}

try {
FileOutputStream fos = new FileOutputStream(f);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
} catch(Exception e){
e.printStackTrace();
}
System.out.println(这是我看到的文本文件,但不是在CONSOLE);

for(int i = 0; i <10; i ++){

System.out.println(Testing);
}

}

}




因为你想在两个流中写入数据,所以尝试使用 TeeOutputStream code> 来自 Apache Commons 。第二次更改您的代码尝试

  try {
FileOutputStream fos = new FileOutputStream(f);
//我们将打印在标准的System.out和文件
TeeOutputStream myOut = new TeeOutputStream(System.out,fos);
PrintStream ps = new PrintStream(myOut);
System.setOut(ps);
} catch(Exception e){
e.printStackTrace();
}

现在来自 System.out 也将放置在您的文件中。



只要确保在应用程序完成之前关闭流写入文件。


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 second catch block.If you run the code once you will understand what I mean.All I want is to write what is on console to the "omt.txt" file that is all...

After some answers, I see that my question wasn't clear, sorry for that. I want to save console output to omt.txt text file. If on the console "Hello 123" is printed , it should be also in omt.txt file.In other words whatever on the console is printed should be simultaneously written on the om.txt file or can be after the console execution but should be 1-to-1 the same!

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class Wrt_file {

    public static void main(String[] args) {
        System.out.println("THIS is what I see on the console. but not on TEXT file"); 

          File f = new File("omt.txt");
          if(!f.exists())
          {
            try {
                       f.createNewFile();
                } catch (Exception e) {
                    e.printStackTrace();
                }
          }

        try {
                FileOutputStream fos = new FileOutputStream(f);
                PrintStream ps = new PrintStream(fos);
                System.setOut(ps);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("THIS is what I see on the text file, but not on CONSOLE");      

        for (int i=0; i<10; i++){

            System.out.println("Testing");  
        }

    }

}

解决方案

Updated answer after learning that OP wants to duplicate streams

Since you want to write data in both streams try using TeeOutputStream from Apache Commons. Change your code in second try to

try {
    FileOutputStream fos = new FileOutputStream(f);
    //we will want to print in standard "System.out" and in "file"
    TeeOutputStream myOut=new TeeOutputStream(System.out, fos);
    PrintStream ps = new PrintStream(myOut);
    System.setOut(ps);
} catch (Exception e) {
    e.printStackTrace();
}

Now results from System.out will also be placed in your file.

Just be sure to close stream writing to file before when application will finish.

这篇关于写入控制台和文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆