Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java how to assign value to System.in? [duplicate]

Tags:

java

Possible Duplicate:
Writing data to System.in

We know that System.in (Standard Input) is connected to console. So whenever we write in console it will flow to this stream. But is there any way to pass value to this Standard Input without entering from console, i.e. like System.in = "ABCD". I just want to imitate as the value is passing from console.

like image 217
Angom Avatar asked Oct 23 '12 10:10

Angom


1 Answers

Yes, there is. Use System.setIn(InputStream in).

You can supply any subtype of InputStream as well, so if you want to supply a specific value, you can use the StringBufferInputStream, like so:

StringBufferInputStream s = new StringBufferInputStream("ABCD");
System.setIn(s);
like image 133
mthmulders Avatar answered Oct 24 '22 03:10

mthmulders