"์๋ ํ์ธ์, 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! ์ด๊ฒ์ ๋ด๊ฐ ๋ณธ ๊ฒ ์ค ๊ฐ์ฅ ํฅ๋ฏธ๋ก์ด ์์ ๋๋ค. ๋น์ ์ด ๊ทธ๋ ๊ฒ ํ ์ ์๋์ง ๋ชฐ๋์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค."
"์ฒ๋ง์์, ์๋ฏธ๊ณ ."
GO TO FULL VERSION