java IO流,通过转换流实现键盘输入屏幕输出

测试转换流对象InputStreamReader、OutputStreamWriter,注意:System.in是InputStream,System.out是PrintStream,PrintStream extends FilterOutputStream,FilterOutputStream extends OutputStream

 

import java.io.*;

public class ConvertStream {
    public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            bw = new BufferedWriter(new OutputStreamWriter(System.out));
            while(true){
                bw.write("请输入:");
                bw.flush();
                String input = br.readLine();
                if(input.equals("exit")){
                    break; //输入exit就结束
                }
                bw.write("你输入的是:"+input);
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null){
                    bw.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

posted @ 2021-09-15 12:10  meteor119  阅读(93)  评论(0编辑  收藏  举报