csetariq
BAN USERYes, you can create your own PrintStream and set it as the output stream of your program
package com.test;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
System.setOut(new PrintStream("outfile.txt"));
System.out.println("Hello, World!");
}
}
out
in
System
is the output stream of a java process, you can redirect it to a file with redirection operator
java ClassName >/file/you/want/to/save/to
If you want to redirect the error stream to a file, use
java ClassName 2>/file/you/want/to/save/to
For any CLI process there are at least 3 streams associated with it,
stdin 0
stdout 1
stderr 2
0,1,2 are the file descriptor of the respective streams
- csetariq September 09, 2014
Works for languages like C
- csetariq August 03, 2015