萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 699|回复: 0

1-1-10-3 请问有谁知道如何将System.out重定向输出到一个JT

[复制链接]
发表于 2003-2-6 21:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

x

  1. 发信人: ttttt (如果), 信区: Java      
  2. 标  题: Re: 请问有谁知道如何将System.out重定向输出到一个JT
  3. 发信站: BBS 水木清华站 (Mon Oct 25 14:08:03 1999)

  4. 【 在 oldcrab  (老蟹) 的大作中提到: 】
  5. : 请问有谁知道如何将System.out重定向输出到一个JTextArea?
  6. : 谢谢!

  7. 希望这个能帮你忙,呵呵

  8. CAPTURING STANDARD OUTPUT IN A LOG FILE
  9. This tip demonstrates how you can record in a log file everything you
  10. print to standard out and standard error. This is especially useful
  11. if you deploy an application and your users encounter problems. You
  12. can have the users send you the log file for analysis.
  13. The following example code demonstrates how to capture standard
  14. output. You can include the example code, as is, in your program.
  15. The example implements a class called SaveOutput, with two static
  16. methods - start() and stop().  Calling start() creates a new log
  17. file or empties an existing log file. It copies into the log file
  18. characters printed to standard output and standard error.  Calling
  19. stop() closes the log file and restores the behavior of standard
  20. output and standard error (that is, their behavior before start() was
  21. called).
  22. import java.io.*;
  23. class Stdout {
  24.     public static void main(String[] args) {
  25.         try {
  26.             // Start capturing characters into the log file.
  27.             SaveOutput.start("log.txt");
  28.             // Test it.
  29.             System.out.println("Here's is some stuff to stdout.");
  30.             System.err.println("Here's is some stuff to stderr.");
  31.             System.out.println("Let's throw an exception...");
  32.             new Exception().printStackTrace();
  33.         } catch (Exception e) {
  34.             e.printStackTrace();
  35.         } finally {
  36.             // Stop capturing characters into the log file
  37.             // and restore old setup.
  38.             SaveOutput.stop();
  39.         }
  40.     }
  41. }
  42. class SaveOutput extends PrintStream {
  43.     static OutputStream logfile;
  44.     static PrintStream oldStdout;
  45.     static PrintStream oldStderr;
  46.     SaveOutput(PrintStream ps) {
  47.         super(ps);
  48.     }
  49.     // Starts copying stdout and stderr to the file f.
  50.     public static void start(String f) throws IOException {
  51.         // Save old settings.
  52.         oldStdout = System.out;
  53.         oldStderr = System.err;
  54.         // Create/Open logfile.
  55.         logfile = new PrintStream(
  56.             new BufferedOutputStream(
  57.             new FileOutputStream(f)));
  58.         // Start redirecting the output.
  59.         System.setOut(new SaveOutput(System.out));
  60.         System.setErr(new SaveOutput(System.err));
  61.     }
  62.     // Restores the original settings.
  63.     public static void stop() {
  64.         System.setOut(oldStdout);
  65.         System.setErr(oldStderr);
  66.         try {
  67.             logfile.close();
  68.         } catch (Exception e) {
  69.             e.printStackTrace();
  70.         }
  71.     }
  72.     // PrintStream override.
  73.     public void write(int b) {
  74.         try {
  75.             logfile.write(b);
  76.         } catch (Exception e) {
  77.             e.printStackTrace();
  78.             setError();
  79.         }
  80.         super.write(b);
  81.     }
  82.     // PrintStream override.
  83.     public void write(byte buf[], int off, int len) {
  84.         try {
  85.             logfile.write(buf, off, len);
  86.         } catch (Exception e) {
  87.             e.printStackTrace();
  88.             setError();
  89.         }
  90.         super.write(buf, off, len);
  91.     }
  92. }
  93. The start() method first saves the current standard output and
  94. standard error print streams.  These print streams will be restored
  95. when stop() is called. Next, the log file is opened.  If the log
  96. file does not exist, it's created.  Otherwise, the log file is
  97. emptied.  Finally, System.setOut() and System.setErr() are called
  98. to replace the standard output and standard error print streams with
  99. SaveOutput print streams.
  100. The stop () method restores the original standard output and
  101. standard error. It then closes the log file.
  102. A SaveOutput object is a PrintStream object that acts like a tee.
  103. Any characters it receives are forwarded to two places: the log file
  104. and the underlying print streams. The underlying print streams in
  105. the example are the original standard output and standard error
  106. print streams; these are supplied to the SaveOutput constructor.
  107. Although both standard output and standard error are written into the
  108. same logfile, there is no need to synchronize this operation.  The
  109. reason is that the logfile output stream is itself a print stream
  110. and write operations are synchronized.
  111. To implement this tee behavior, the program needs to override the
  112. two forms of the write method. These overrides simply write the
  113. characters into the logfile and then to the underlying print stream
  114. (by calling super.write()). The write() methods do not throw
  115. exceptions. Instead, they set a flag in the print stream if some
  116. problem occurs. They set the flag by calling setError(). If the client
  117. of the print stream wants to check if an error occurred, it can call
  118. checkError().
复制代码
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网

GMT+1, 2025-2-15 04:30 , Processed in 0.059804 second(s), 19 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表