Re: [問題] 如何將"system.out的輸出"轉成string
※ 引述《pa015596 (sdfgdgf)》之銘言:
: 雖然這樣做有多此一舉的感覺
: 但因為我是套用別人的API
: 他的API會直接將結果印到螢幕上
: 而我需要將它的結果在繼續處理,需要轉成string
: 我是一個java的小小新手....希望大大幫忙了
: 感激不盡
sample code:
-------------------------------------------------------------------------
import java.io.*;
public class RedirectOut {
public static void main(String[] args) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream psOld = System.out;
PrintStream psNew = null;
String s = null;
try{
psNew = new PrintStream(baos);
System.setOut(psNew);
//Call API
s = baos.toString();
System.setOut(psOld);
}
catch(Exception ex){
System.exit(-1);
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.62.64.95
※ 編輯: pico2k 來自: 61.62.64.95 (08/29 09:12)
※ 編輯: pico2k 來自: 61.62.64.95 (08/29 09:15)
推
08/29 14:07, , 1F
08/29 14:07, 1F
推
08/29 14:22, , 2F
08/29 14:22, 2F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):