重置标准输入输出流 IO流

2015/01/24 21:19
阅读数 95
public class TransStreamDemo2 {
	public static void main(String[] args) {
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			/*
			 效果等同于复制:	
			重新设置标准输入输出流
			System.setIn(new FileInputStream("D:"+File.separator+"a.txt"));
			System.setOut(new PrintStream("D:"+File.separator+"b.txt"));
			 */
			br = new BufferedReader(new InputStreamReader(System.in));
			bw = new BufferedWriter(new OutputStreamWriter(System.out));

			String buf = null;
			while((buf = br.readLine()) != null){
				bw.write(buf);
				bw.newLine();
			}
		} catch (IOException e) {
			throw new RuntimeException("文件操作失败");
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException e) {
				throw new RuntimeException("流关闭失败");
			}
			try {
				if (bw != null)
					bw.close();
			} catch (IOException e) {
				throw new RuntimeException("流关闭失败");
			}
		}

	}
}


展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部