import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class stream {
public static void main(String args[]) throws IOException
{
PrintStream oldPrintStream = System.out;
File file = new File("c://tex.txt");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
System.setOut(new PrintStream(bos));
System.out.println("this is the text to output 汗字");
System.setOut(oldPrintStream);
System.out.println(bos.toString()+"asdasd");
FileOutputStream fs = new FileOutputStream(file);
byte bytes[] = bos.toString().getBytes();
fs.write(bytes);
fs.close();
}
}