public class Frame1 extends JFrame implements ActionListener
{
BorderLayout borderLayout1 = new BorderLayout();
JTextArea jTextArea1 = new JTextArea();
JScrollPane jScrollPane1 = new JScrollPane();
JButton jButton1 = new JButton();
JPanel jPanel1 = new JPanel();
PrintStream orignalOut;
OutputStream jtextOut;
//Construct the frame
public Frame1() {
//保存原始标准输出流
orignalOut = System.out;
//创建JTextArea输出流,用于重定向
jtextOut = new OutputStream()
{
final int BUFFER_LENGTH = 1024;
byte buf[] = new byte[BUFFER_LENGTH];
int pos = 0;
public void write(int b) throws IOException
{
buf[pos ++] = (byte)b;
if (pos >= BUFFER_LENGTH)
flush();
}
public void flush() throws IOException
{
if (pos >= BUFFER_LENGTH)
jTextArea1.append(new String(buf));
else
jTextArea1.append(new String(buf, 0, pos));
//Overridden so we can exit on System Close
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
boolean writeToConsole = true;
//按钮处理函数
public void actionPerformed(ActionEvent e)
{
if (e.getSource ()== this.jButton1)
{
if (writeToConsole)
{
// 将标准输出重定向到JTextArea
jButton1.setText("write to console");
// true表示自动调用flush
System.setOut(new PrintStream(jtextOut, true));
writeToConsole = false;
}
else
{
// 将标准输出恢复到原来
jButton1.setText("write to JTextArea");
System.setOut(orignalOut);
writeToConsole = true;
}
}
}
public static void main(String args[])
{
Frame1 frame = new Frame1();
frame.setVisible(true);
//下面这个线程每个0.5秒向标准输出打印一个字符串
new Thread (){
public void run()
{
while(true)
{
System.out.println("asdfasdf");
try{
Thread.sleep(500);
}catch(Exception e){}
}
}
}.start();
public class Redirect
{
public static void main (String args[])
{
// Save the current standard input, output, and error streams
// for later restoration.
InputStream origIn = System.in;
PrintStream origOut = System.out;
PrintStream origErr = System.err;
// Create a new input stream from a file.
InputStream stdin = null;
try
{
stdin = new FileInputStream ("Redirect.in");
}
catch (Exception e)
{
// Sigh. Couldn't open the file.
System.out.println ("Redirect: Unable to open input file!");
System.exit (1);
}
// Create a new output stream for the standard output.
PrintStream stdout = null;
try
{
stdout = new PrintStream (new FileOutputStream
("Redirect.out"));
}
catch (Exception e)
{
// Sigh. Couldn't open the file.
System.out.println ("Redirect: Unable to open output file!");
System.exit (1);
}
// Create new output stream for the standard error output.
PrintStream stderr = null;
try
{
stderr = new PrintStream (new FileOutputStream
("Redirect.err"));
}
catch (Exception e)
{
// Sigh. Couldn't open the file.
System.out.println ("Redirect: Unable to open error file!");
System.exit (1);
}
// Print stuff to the original output and error streams.
// On most systems all of this will end up on your console when you
// run this application.
origOut.println ("\nRedirect: Round #1");
System.out.println ("Test output via 'System.out'.");
origOut.println ("Test output via 'origOut' reference.");
System.err.println ("Test output via 'System.err'.");
origErr.println ("Test output via 'origErr' reference.");
// Set the System out and err streams to use our replacements.
System.in = stdin;
System.out = stdout;
System.err = stderr;
// Print stuff to the original output and error streams.
// The stuff printed through the 'origOut' and 'origErr' references
// should go to the console on most systems while the messages
// printed through the 'System.out' and 'System.err' will end up in
// the files we created for them.
origOut.println ("\nRedirect: Round #2");
System.out.println ("Test output via 'System.out'.");
origOut.println ("Test output via 'origOut' reference.");
System.err.println ("Test output via 'System.err'.");
origErr.println ("Test output via 'origErr' reference.");
// Read some input and dump it to the console.
origOut.println ("\nRedirect: Round #3");
int inChar = 0;
while (-1 != inChar)
{
try
{
inChar = System.in.read();
}
catch (Exception e)
{
// Clean up the output and bail.
origOut.print ("\n");
break;
}
origOut.write (inChar);
}
// Close the streams.
try
{
stdin.close ();
stdout.close ();
stderr.close ();
}
catch (Exception e)
{
origOut.println ("Redirect: Unable to close files!");
System.exit (1);
}