2009-02-19 6 views

risposta

25

Non è possibile farlo esattamente, poiché StringWriter è un Writer, non un Stream. Ma puoi fare questo:

// create a ByteArray stream, which will be wrapped by a PrintStream 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
PrintStream ps = new PrintStream(baos); 
System.setOut(ps); 

// print whatever you got 
String result = baos.toString(); 
+1

+1 - più cancellato il mio post duplicato – toolkit