CodeGym /Kursus Jawa /Inti Jawa /Ayo nulis pambungkus dhewe kanggo System.out

Ayo nulis pambungkus dhewe kanggo System.out

Inti Jawa
tingkat , wulangan
kasedhiya

"Halo, Amigo! Dina iki sampeyan bakal sinau carane nindakake soko anyar: ngganti obyek System.out."
System.out minangka variabel PrintStream statis sing diarani ing kelas Sistem . Variabel iki nduweni modifikasi pungkasan , dadi sampeyan mung menehi nilai anyar. Nanging, kelas Sistem duwe cara khusus kanggo nindakake iki: setOut(PrintStream stream) . Lan sing bakal kita gunakake.

"Menarik. Lan apa sing bakal kita ganti?"

"We need sawetara obyek sing bisa ngumpulake data output. ByteArrayOutputStream paling cocok kanggo proyek iki. Iki kelas khusus sing dinamis (bisa diowahi ukurane) Uploaded lan ngleksanakake antarmuka OutputStream."

"Adaptor antarane larik lan OutputStream?"

"Sing kaya ngono. Iki kodhe kita."

Kode
public static void main(String[] args) throws Exception
{
 //Save the current PrintStream in a special variable
 PrintStream consoleStream = System.out;

 //Create a dynamic array
 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 //Create an adapter for the PrintStream class
 PrintStream stream = new PrintStream(outputStream);
 //Set it as the current System.out
 System.setOut(stream);

 //Call a function that knows nothing about our changes
 printSomething();

 //Convert the data written to our ByteArray into a string
 String result = outputStream.toString();

 //Put everything back to the way it was
 System.setOut(consoleStream);
}

public static void printSomething()
{
 System.out.println("Hi");
 System.out.println("My name is Amigo");
 System.out.println("Bye-bye!");
}

"Lan apa sing bakal kita tindakake karo baris iki?"

"Inggih, apa wae sing dikarepake. Contone, kita bakal mbalikke. Banjur kode kasebut katon kaya iki: "

Kode
public static void main(String[] args) throws Exception
{
 //Save the current PrintStream in a special variable
 PrintStream consoleStream = System.out;

 //Create a dynamic array
 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 //Create an adapter for the PrintStream class
 PrintStream stream = new PrintStream(outputStream);
 //Set it as the current System.out
 System.setOut(stream);

 //Call a function that knows nothing about our changes
 printSomething();

 //Convert the data written to our ByteArray into a string
 String result = outputStream.toString();

 //Put everything back to the way it was
 System.setOut(consoleStream);

 //Reverse the string
 StringBuilder stringBuilder = new StringBuilder(result);
 stringBuilder.reverse();
 String reverseString = stringBuilder.toString();

 //Output it to the console
 System.out.println(reverseString);
}

public static void printSomething()
{
 System.out.println("Hi");
 System.out.println("My name is Amigo");
 System.out.println("Bye-bye!");
}

"Carane menarik! Saiki aku wiwit ngerti sethithik babagan kemampuan gedhe sing diwenehake dening kelas cilik iki."
"Matur nuwun kanggo pelajaran sing menarik, Bilaabo."

Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION