BufferedReader readLine()方法

时间:2021-11-12 06:49:04

控制台输入字符串之后回车,后台接收传来的字符串,代码如下:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;  

    public class ReadLineDemo {  

        public static void main(String[] args) {
            // System.in是获取键盘输入的值
            // InputStreamReader将字节流转为字符流 转为BufferedReader
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String line;
            try {
                //readLine()是阻塞方法,当键盘输入之后点击回车,否则就一直处于阻塞状态
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }