"Hello, Amigo! Ngayon ay matututo ka kung paano gumawa ng bago: palitan ang System.out object."
Ang System.out ay isang static na variable ng PrintStream na tinatawag sa klase ng System . Ang variable na ito ay may panghuling modifier, kaya itatalaga mo lang ito ng isang bagong halaga. Gayunpaman, ang klase ng System ay may espesyal na paraan para gawin ito: setOut(PrintStream stream) . At iyon ang gagamitin namin.

"Interesting. At ano ang ipapalit natin?"

"Kailangan namin ng ilang bagay na maaaring mangolekta ng data ng output. Ang ByteArrayOutputStream ay pinakaangkop para sa trabahong ito. Ito ay isang espesyal na klase na isang dynamic (resizable) array at nagpapatupad ng OutputStream interface."

"Isang adaptor sa pagitan ng array at OutputStream?"

"May ganyan. Ganito ang itsura ng code natin."

Code
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!");
}

"At ano ang gagawin natin sa linyang ito?"

"Well, kahit anong gusto namin. Halimbawa, ibabalik namin ito. Pagkatapos ay magiging ganito ang code:"

Code
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!");
}

"Nakakainteres! Ngayon ay nagsisimula na akong maunawaan ang kaunti sa magagandang kakayahan na ibinibigay ng maliliit na klase na ito."
"Salamat sa kawili-wiling aral, Bilaabo."