BufferedReader | PrintStream
1 重定向标准输入流
2 重定向标准输出流
package pb.io.reio;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
/**
* 重定向标准输入流
* @author TerryZhong
*
*/
public class ReIn {
public static void main(String[] args) {
try {
FileInputStream fis=new FileInputStream("d:/test/myPoem.txt");
//重定向标准输入流
System.setIn(fis);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//输出内容
String line="";
try {
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();//关闭流
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
System.out.println("文件没找到!");
}
}
}
package pb.io.reio;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
/**
* 重定向标准输出流
* @author TerryZhong
*
*/
public class ReOut {
public static void main(String[] args) {
try {
//创建PrintStream对象
PrintStream ps = new PrintStream(new FileOutputStream("d:/test/myPoem.txt"));
//重定向输出流
System.setOut(ps);
//向流中写信息
System.out.println("静夜思\r\n床前明月光,\r\n凿壁学java。\r\n举头望明月,\r\n低头啃I/O。");
//关闭流
ps.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}