Writing data to System.in

前端 未结 4 1106
感情败类
感情败类 2020-12-14 21:42

In our application, we expect user input within a Thread as follows :

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         


        
4条回答
  •  青春惊慌失措
    2020-12-14 22:26

    Replace it for the duration of your test:

    String data = "the text you want to send";
    InputStream testInput = new ByteArrayInputStream( data.getBytes("UTF-8") );
    InputStream old = System.in;
    try {
        System.setIn( testInput );
    
        ...
    } finally {
        System.setIn( old );
    }
    

提交回复
热议问题