"안녕하세요, 아미고! 오늘은 System.out 객체를 대체하는 새로운 작업을 수행하는 방법을 배우게 될 것입니다."
System.out은 System 클래스 에서 호출 되는 정적 PrintStream 변수 입니다 . 이 변수에는 최종 수정자가 있으므로 새 값을 할당하기만 하면 됩니다. 그러나 System 클래스에는 이를 수행하는 특별한 메서드가 있습니다: setOut(PrintStream stream) . 그리고 그것이 우리가 사용할 것입니다.

"흥미롭군. 그러면 무엇으로 대체할 것인가?"

"출력 데이터를 수집할 수 있는 개체가 필요합니다. ByteArrayOutputStream이 이 작업에 가장 적합합니다. 이것은 동적(크기 조정 가능) 배열이고 OutputStream 인터페이스를 구현하는 특수 클래스입니다."

"배열과 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!");
}

"흥미롭군요! 이제 저는 이 소규모 클래스가 제공하는 훌륭한 기능을 조금 이해하기 시작했습니다."
"흥미로운 수업 감사합니다, Bilaabo."