java 写入控制台和文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16237546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing to console and text file
提问by Anarkie
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...
我从互联网上找到了下面的代码,但它没有将打印的控制台写入 omt.txt,它只System.out.println
在第二个 catch 块之后写入语句。如果你运行代码一次你就会明白我的意思。所有我想要的是将控制台上的内容写入“omt.txt”文件中,这就是所有...
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!
经过一些回答后,我发现我的问题不清楚,抱歉。我想将控制台输出保存到 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("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");
}
}
}
回答by Pshemo
Updated answer after learning that OP wants to duplicate streams
得知 OP 想要复制流后更新了答案
Since you want to write data in both streams try using TeeOutputStream
from Apache Commons. Change your code in second try to
由于您想在两个流中写入数据,请尝试使用TeeOutputStream
from Apache Commons。在第二次尝试更改您的代码
try {
FileOutputStream fos = new FileOutputStream(f);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
fos.flush();
}
catch (Throwable t) {
// Ignore
}
}, "Shutdown hook Thread flushing " + 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, true); //true - auto-flush after println
System.setOut(ps);
} catch (Exception e) {
e.printStackTrace();
}
Now results from System.out
will also be placed in your file.
现在结果System.out
也将放置在您的文件中。
回答by Alya'a Gamal
The reason is :
原因是 :
The java.lang.System.setOut()
method reassigns the "standard" output stream.
The java.lang.System.setOut()
方法重新分配“标准”输出流。
so when you use System.out.println
it will print only in the text file
所以当你使用System.out.println
它时只会在文本文件中打印
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");
}
回答by Jops
In System.java
, this is the declaration of the out
property:
在 中System.java
,这是out
属性的声明:
public final static PrintStream out
You'll see that it can only be one PrintSteam object at a time. So it's either the console or the file, but not both.
您会看到它一次只能是一个 PrintSteam 对象。所以它要么是控制台,要么是文件,但不是两者兼而有之。
At this line, you have effectively re-channelled the destination:
在这一行,你有效地重新引导了目的地:
System.setOut(ps);
So your output stops displaying on the console.
所以你的输出停止显示在控制台上。