"Hello, Amigo! Today we'll explore some super interesting stuff: how to replace System.in input stream."

System.in is a simple static InputStream variable, but you can't simply assign a new value to it. But you can use the System.setIn() method.

First, we need to create a buffer, and then put some values into it. Then we'll wrap it in a class that knows how to read data from the buffer using the InputStream protocol.

This is how it looks:

Code
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! This is the most interesting example I've seen. I didn't know you could do that. Thanks."

"You're welcome, Amigo."