"हैलो, अमीगो! आज आप कुछ नया करना सीखेंगे: System.out ऑब्जेक्ट को बदलें।"
System.out एक स्टैटिक PrintStream वेरिएबल है जिसे System क्लास में कॉल किया जाता है। इस चर में अंतिम संशोधक है, इसलिए आप इसे केवल एक नया मान निर्दिष्ट करते हैं। हालाँकि, सिस्टम वर्ग के पास ऐसा करने के लिए एक विशेष तरीका है: setOut(PrintStream stream) । और हम यही प्रयोग करेंगे।

"दिलचस्प। और हम इसे किससे बदलेंगे?"

"हमें कुछ ऑब्जेक्ट की आवश्यकता है जो आउटपुट डेटा एकत्र कर सके। ByteArrayOutputStream इस नौकरी के लिए सबसे उपयुक्त है। यह एक विशेष वर्ग है जो गतिशील (आकार बदलने योग्य) सरणी है और आउटपुटस्ट्रीम इंटरफ़ेस लागू करता है।"

"एक सरणी और OutputStream के बीच एक अनुकूलक?"

"ऐसा कुछ। यहां हमारा कोड कैसा दिखता है।"

कोड
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!");
}

"और हम इस लाइन के साथ क्या करेंगे?"

"ठीक है, हम जो भी चाहते हैं। उदाहरण के लिए, हम इसे उल्टा कर देंगे। फिर कोड इस तरह दिखेगा:"

कोड
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!");
}

"कितना दिलचस्प है! अब मैं इन छोटी कक्षाओं द्वारा प्रदान की जाने वाली महान क्षमताओं के बारे में थोड़ा-बहुत समझने लगा हूँ।"
"दिलचस्प पाठ के लिए धन्यवाद, बिलाबो।"