如何将控制台输出写入txt文件 [英] How to write console output to a txt file

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

问题描述

我尝试使用此代码建议将控制台输出写入 txt 文件 (http://www.daniweb.com/forums/thread23883.html#) 但是我没有成功.怎么了?

I have tried to write the console output to a txt file using this code suggestion (http://www.daniweb.com/forums/thread23883.html#) however I was not successful. What's wrong?

try {
      //create a buffered reader that connects to the console, we use it so we can read lines
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

      //read a line from the console
      String lineFromInput = in.readLine();

      //create an print writer for writing to a file
      PrintWriter out = new PrintWriter(new FileWriter("output.txt"));

      //output to the file a line
      out.println(lineFromInput);

      //close the file (VERY IMPORTANT!)
      out.close();
   }
      catch(IOException e1) {
        System.out.println("Error during reading/writing");
   }

推荐答案

你需要做这样的事情:

PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);

第二个语句是关键.它将所谓的最终"System.out 属性值更改为提供的 PrintStream 值.

The second statement is the key. It changes the value of the supposedly "final" System.out attribute to be the supplied PrintStream value.

有类似的方法(setInsetErr)来改变标准输入和错误流;有关详细信息,请参阅 java.lang.System javadocs.

There are analogous methods (setIn and setErr) for changing the standard input and error streams; refer to the java.lang.System javadocs for details.

以上更通用的版本是这样的:

A more general version of the above is this:

PrintStream out = new PrintStream(
        new FileOutputStream("output.txt", append), autoFlush);
System.setOut(out);

如果 appendtrue,流将附加到现有文件而不是截断它.如果autoflushtrue,则每当写入字节数组、调用println 方法之一或<代码> 写好了.

If append is true, the stream will append to an existing file instead of truncating it. If autoflush is true, the output buffer will be flushed whenever a byte array is written, one of the println methods is called, or a is written.

我只想补充一点,使用诸如 Log4jLogback 或标准 Java java.util.logging 子系统.它们通过运行时配置文件提供细粒度的日志记录控制、对滚动日志文件的支持、对系统日志记录的馈送等.

I'd just like to add that it is usually a better idea to use a logging subsystem like Log4j, Logback or the standard Java java.util.logging subsystem. These offer fine-grained logging control via runtime configuration files, support for rolling log files, feeds to system logging, and so on.

或者,如果您不记录",请考虑以下事项:

Alternatively, if you are not "logging" then consider the following:

  • 使用典型的 shell,您可以将标准输出(或标准错误)重定向到命令行上的文件;例如

  • With typical shells, you can redirecting standard output (or standard error) to a file on the command line; e.g.

$ java MyApp > output.txt   

有关详细信息,请参阅 shell 教程或手册条目.

For more information, refer to a shell tutorial or manual entry.

您可以将应用程序更改为使用作为方法参数传递的 out 流,或者通过单例或依赖项注入而不是写入 System.out.

You could change your application to use an out stream passed as a method parameter or via a singleton or dependency injection rather than writing to System.out.

更改 System.out 可能会导致 JVM 中其他不希望发生这种情况的代码出现令人讨厌的意外.(设计得当的 Java 库将避免依赖于 System.outSystem.err,但你可能不走运.)

Changing System.out may cause nasty surprises for other code in your JVM that is not expecting this to happen. (A properly designed Java library will avoid depending on System.out and System.err, but you could be unlucky.)

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

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