-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy path输入输出重定向.txt
43 lines (36 loc) · 909 Bytes
/
输入输出重定向.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.OutputStream;
import java.io.PrintStream;
public class TestSetIO {
/**
* Ìí¼Ó×¢ÊÍ
*
* Returns ·µ»ØÃèÊö
*/
public static void main(String[] args) {
PrintStream ps = System.out;
TestSetIO setIO = new TestSetIO();
PrintStream out = new FakePrintStream(System.out);
System.setOut(out);
setIO.printMethod();
String tmpStr = ((FakePrintStream)out).getStr();
System.setOut(ps);
System.out.println("FakePrintStream : " + tmpStr);
}
private void printMethod() {
System.out.println(" in printMethod ");
}
}
class FakePrintStream extends PrintStream{
private StringBuilder str = new StringBuilder();
public FakePrintStream(OutputStream out){
super(out);
}
@Override
public void write(byte buf[], int off, int len){
str.append(new String(buf, off, len));
}
public String getStr(){
return str.toString();
}
}
--------------------------------------------------------------------------------