请问下边这段代码的运行流程是啥?

运行代码,在控制台输入字符点回车会被打印,但是控制台上的字符是如何传入这段逻辑代码的呢,请大神指点!

package basic;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

public class Piped {

    public static void main(String[] args) throws Exception {
        PipedWriter out = new PipedWriter();
        PipedReader in = new PipedReader();
        out.connect(in);
        Thread printThread = new Thread(new Print(in), "PrintThread");
        printThread.start();
        int receive = 0;

        try {
            while((receive = System.in.read())!=-1) {
                out.write(receive);
            }
        } finally {
            out.close();
        }
    }
    static class Print implements Runnable{
        private PipedReader in;

        public Print(PipedReader in) {
            this.in = in;
        }

        public void run() {
            int receive = 0;
            try {
                while((receive = in.read())!=-1) {
                    System.out.println((char)receive);
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }



}

3个回答

System.in.read()

while((receive = in.read())!=-1)
这里就是读取

输入流读取你输入的字符然后输出流写出来

 while((receive = System.in.read())!=-1) {
                out.write(receive);
            }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问