자바 콘 솔 출력 지점 변경

755 단어
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

/**
 * TestSysOut.java
 */

/**
 * TestSysOut
 * 
 * @author qefee
 * 
 */
public class TestSysOut {

	/**
	 * main.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		ByteArrayOutputStream baoStream = new ByteArrayOutputStream(1024);
		// cache stream
		PrintStream cacheStream = new PrintStream(baoStream);
		// old stream
		PrintStream oldStream = System.out;

		System.setOut(cacheStream);

		System.out.print("hello world!");

		String message = baoStream.toString();

		message = "<-- " + message + " -->";

		// Restore old stream
		System.setOut(oldStream);

		System.out.println(message);
	}
}

좋은 웹페이지 즐겨찾기