"Xin chào, Amigo! Hôm nay chúng ta sẽ khám phá một số nội dung cực kỳ thú vị: cách thay thế System.in input stream ."

System.in  là một biến InputStream tĩnh đơn giản , nhưng bạn không thể chỉ định một giá trị mới cho nó. Nhưng bạn có thể sử dụng phương thức System.setIn().

Đầu tiên, chúng ta cần tạo một bộ đệm, sau đó đặt một số giá trị vào đó. Sau đó, chúng tôi sẽ bọc nó trong một lớp biết cách đọc dữ liệu từ bộ đệm bằng giao thức InputStream.

Cái này nó thì trông như thế nào:

Mã số
public static void main(String[] args) throws IOException
{
 //Put data into a string
 StringBuilder sb = new StringBuilder();
 sb.append("Lena").append('\n');
 sb.append("Olya").append('\n');
 sb.append("Anya").append('\n');
 String data = sb.toString();

 //Wrap the string in a ByteArrayInputStream
 InputStream is = new ByteArrayInputStream(data.getBytes());

 //Replace in
 System.setIn(is);

 //Call an ordinary method that doesn't know about our changes
 readAndPrintLine();
}

public static void readAndPrintLine() throws IOException
{
 InputStreamReader isr = new InputStreamReader(System.in);
 BufferedReader reader = new BufferedReader(isr);

 while (true)
 {
  String line = reader.readLine();
  if (line == null) break;
  System.out.println(line);
 }
 reader.close();
 isr.close();
}

"Bilaabo! Đây là ví dụ thú vị nhất mà tôi từng thấy. Tôi không biết bạn có thể làm được điều đó. Cảm ơn."

"Không có chi, Amigo."