如何将控制台输出写入 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.
有类似的方法(setIn
和 setErr
)来改变标准输入和错误流;有关详细信息,请参阅 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);
如果 append
为 true
,则流将附加到现有文件而不是截断它.如果 autoflush
为 true
,则每当写入字节数组、调用 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.
我想补充一点,使用像 Log4j这样的日志子系统通常是一个更好的主意a>、Logback 或标准 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.out
和 System.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.)
相关文章