"안녕하세요, Amigo! 오늘 우리는 System.in 입력 스트림을 교체하는 방법과 같은 몇 가지 매우 흥미로운 내용을 살펴보겠습니다 ."

System.in  은 간단한 정적 InputStream 변수이지만 단순히 새 값을 할당할 수는 없습니다. 그러나 System.setIn() 메서드를 사용할 수 있습니다 .

먼저 버퍼를 생성한 다음 일부 값을 버퍼에 입력해야 합니다. 그런 다음 InputStream 프로토콜을 사용하여 버퍼에서 데이터를 읽는 방법을 알고 있는 클래스로 래핑합니다.

다음과 같이 표시됩니다.

암호
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! 이것은 내가 본 것 중 가장 흥미로운 예입니다. 당신이 그렇게 할 수 있는지 몰랐습니다. 감사합니다."

"천만에요, 아미고."