写真:
私は、ファイルに標準出力を保存したいです。このために、私が使用します
System.setOut(new PrintStream(new File("output-file.txt")));
さて、コンソールの出力はありません。
try {
System.setOut(new PrintStream(new File("output-file.txt")));
} catch (Exception e) {
e.printStackTrace();
}
私は、ファイルを埋めるために標準出力を使用していますが、コンソールに標準出力を表示するいずれかの可能性がありますか?
TiiJ7:
あなたは、作成することができPrintWriter
てOutputStream
、両方の書き込みをしています。
final OutputStream stdOut = System.out;
try {
System.setOut(new PrintStream(new OutputStream() {
private PrintStream ps = new PrintStream(new File("output-file.txt"));
@Override
public void write(int b) throws IOException {
ps.write(b);
stdOut.write(b);
}
@Override
public void flush() throws IOException {
super.flush();
ps.flush();
stdOut.flush();
}
@Override
public void close() throws IOException {
super.close();
ps.close();
// stdOut.close(); // Normally not done
}
}));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Hello, world!");