2010-05-21 4 views
6

일부 정보를 화면에 출력하는 display() 함수가있는 클래스가 있습니다. 나는 그것을 바꿀 수 없다. 그것이 화면에 인쇄하는 문자열을 외부로 "잡을"수있는 방법이 있습니까?Java에서 화면 캡처하기

콘솔에 표시됩니다.

+0

어떻게 인쇄하나요? 그래픽으로 또는 콘솔로? – aioobe

답변

5

내가 접할 수있는 가장 가까운 것은 System.out을 통해 인쇄 된 모든 것을 포착하고 전달하는 것입니다.

setOut(java.io.PrintStream) 방법을 살펴보십시오.

완전한 예는 다음과 같습니다

import java.io.PrintStream; 

public class Test { 

    public static void display() { 
     System.out.println("Displaying!"); 
    } 

    public static void main(String... args) throws Exception { 
     final List<String> outputLog = new ArrayList<String>(); 
     System.setOut(new PrintStream(System.out) { 
      public void println(String x) { 
       super.println(x); 
       outputLog.add(x); 
      } 

      // to "log" printf calls: 
      public PrintStream printf(String format, Object... args) { 
       outputLog.add(String.format(format, args)); 
       return this; 
      } 
     }); 

     display(); 
    } 
} 
+0

'printf (String format, String)'을 어떻게 사용합니까? –

+0

가로채는 방법을 무시하십시오. (println이 위에 overidden 된 것처럼) – aioobe

+0

원래의'System.out'이 포맷을 처리 한 후에 어떻게 문자열을 얻는가? –

2

내가 표준 디스플레이 자바() 작업에 익숙하지 않은 해요,이 작업중인 프레임 워크에 고유 수 있습니다. 콘솔로 출력됩니까? 메시지 상자를 표시 하시겠습니까?

콘솔에 System.out.println()System.err.println()을 통한 출력물에 대해 이야기하고 있다면 예. 표준 입력 및 표준 출력을 리디렉션 할 수 있습니다. 사용 :

System.setErr(debugStream); 
    System.setOut(debugStream); 

적절한 스트림 (예 : 파일)을 생성하십시오.

관련 문제