【问题标题】:system.out.println redirection in javajava中的system.out.println重定向
【发布时间】:2014-04-10 01:33:50
【问题描述】:

我想捕获 println 并重定向到一个文件并打印它

这是我的代码,我想打印 123 /n 456

但这不起作用,我想在打印 outContent 之前我需要一些东西来阻止捕获,但我不知道该怎么做。

public static void main(String args[]){
    ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    System.setOut(new PrintStream(outContent));
    System.out.println("123");
    System.out.println("456");
    System.out.println(outContent.toString());
}

【问题讨论】:

标签: java


【解决方案1】:

您应该将out 流重定向到这样的文件:

 System.setOut(new PrintStream(new File("<your log file>"));

这将重定向日志文件中的所有system.out。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    Redirect System.out.println to Log4J, while keeping class name information

    上面的帖子在某种程度上解释了如何做到这一点。

    【讨论】:

      【解决方案3】:

      回到过去,在我开始使用 log4j 或其他专用记录器之前,我曾经使用类似于下面的技术。

      本质上它是一个代理PrintStream,它将内容回显到原始PrintStream,并将内容写入其他OutputStream(例如文件)。

      您还可以通过在设置为 true 时不使用 old.write(b) 来设置将回显到控制台的标志。这是我用来阻止应用程序向标准输出喷出大量废话的技术,这可能会减慢应用程序的速度,尤其是当您在图形环境中运行时...

      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.OutputStream;
      import java.io.PrintStream;
      
      public class RedirectStdOut {
      
          public static void main(String[] args) {
      
              OutputStream os = null;
      
              try {
      
                  os = new FileOutputStream(new File("Log.txt"));
                  LoggingPrintStream lps = new LoggingPrintStream(os);
      
                  System.out.println("This is going to the console only...");
                  lps.install();
                  System.out.println("This is going to console and file");
                  System.out.println("Fun times");
                  lps.uinstall();
                  System.out.println("This is going to console only...");
      
              } catch (IOException exp) {
                  exp.printStackTrace();
              } finally {
                  try {
                      os.close();
                  } catch (Exception e) {
                  }
              }
      
          }
      
          public static class LoggingPrintStream {
      
              private OutputStream os;
              private PrintStream old;
      
              public LoggingPrintStream(OutputStream os) {
                  this.os = os;
              }
      
              public void install() {
                  old = System.out;
                  System.setOut(new PrintStream(new OutputStream() {
                      @Override
                      public void write(int b) throws IOException {
                          old.write(b);
                          os.write(b);
                      }
                  }));
              }
      
              public void uinstall() throws IOException {
                  try {
                      os.flush();
                  } finally {
                      try {
                          os.close();
                      } catch (Exception e) {
                      }
                      System.setOut(old);
                  }
              }
      
          }
      
      }
      

      我试图弄清楚我是否可以链接溪流,但今天(星期五下午)我的头脑无法完成任务

      【讨论】:

        猜你喜欢
        • 2011-03-14
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 1970-01-01
        • 2011-10-04
        • 1970-01-01
        相关资源
        最近更新 更多