import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
/**
*
* 标准IO重定向
*
*/
public class IORedirection {
public static void main(String[] args) {
PrintStream console = System.out;
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(new File("d:\\a.txt")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintStream out = null;
try {
out = new PrintStream(new BufferedOutputStream(new FileOutputStream(new File("d:\\a.java").getAbsoluteFile())));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.setIn(in);
System.setOut(out);
System.setErr(out);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = null;
try {
while((s=reader.readLine())!=null){
System.out.println(s);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
out.close();//调用close清除缓存
System.setOut(console);
System.out.println("OK");
}
}
[java][io]标准IO重定向
最新推荐文章于 2021-03-13 11:49:33 发布