"Halo, Amigo! Dina iki kita bakal njelajah sawetara barang super menarik: carane ngganti System.in stream input ."
System.in minangka variabel InputStream statis sing prasaja , nanging sampeyan ora bisa menehi nilai anyar. Nanging sampeyan bisa nggunakake System.setIn () cara.
Pisanan, kita kudu nggawe buffer, banjur sijine sawetara nilai menyang. Banjur kita bakal mbungkus ing kelas sing ngerti carane maca data saka buffer nggunakake protokol InputStream.
Mangkene tampilane:
Kode
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! Iki minangka conto sing paling menarik sing dakdeleng. Aku ora ngerti sampeyan bisa nindakake iku. Thanks."
"Sampun, Amigo."
GO TO FULL VERSION