"হ্যালো, অ্যামিগো! আজ আপনি নতুন কিছু করতে শিখতে যাচ্ছেন: System.out অবজেক্টটি প্রতিস্থাপন করুন।"
System.out হল একটি স্ট্যাটিক প্রিন্টস্ট্রিম ভেরিয়েবল যা সিস্টেম ক্লাসে ডাকা হয় । এই ভেরিয়েবলের চূড়ান্ত সংশোধক রয়েছে, তাই আপনি এটিকে একটি নতুন মান নির্ধারণ করুন। যাইহোক, সিস্টেম ক্লাসের এটি করার জন্য একটি বিশেষ পদ্ধতি রয়েছে: setOut(PrintStream stream) । এবং যে আমরা ব্যবহার করব কি.

"আকর্ষণীয়। এবং আমরা কি দিয়ে এটি প্রতিস্থাপন করব?"

"আমাদের এমন কিছু বস্তু দরকার যা আউটপুট ডেটা সংগ্রহ করতে পারে। ByteArrayOutputStream এই কাজের জন্য সবচেয়ে উপযুক্ত। এটি একটি বিশেষ শ্রেণী যা একটি গতিশীল (রিসাইজযোগ্য) অ্যারে এবং আউটপুট স্ট্রিম ইন্টারফেস প্রয়োগ করে।"

"একটি অ্যারে এবং আউটপুট স্ট্রিমের মধ্যে একটি অ্যাডাপ্টার?"

"এমন কিছু। আমাদের কোড দেখতে কেমন তা এখানে।"

কোড
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!");
}

"কত আকর্ষণীয়! এখন আমি এই ছোট ক্লাসগুলি যে দুর্দান্ত ক্ষমতাগুলি সরবরাহ করে তা কিছুটা বুঝতে শুরু করেছি।"
"আকর্ষণীয় পাঠের জন্য ধন্যবাদ, বিলাবো।"