是否可以在Windows批处理或Java中编写Tee? [英] Is it possible to write a Tee in Windows batch or in Java?

查看:361
本文介绍了是否可以在Windows批处理或Java中编写Tee?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Windows 7控制台中运行的Java程序:

I have a Java program that I run in Windows 7 console:

java -classpath classfolder mypackage.MyProgram

此程序运行时间很长。使用System.out.println将输出写入控制台的时间。

This program runs for very long. Time-by-time it writes output to the console using System.out.println.

是否可以将其输出定向到控制台并导入到日志文件中实时而不修改现有的Java代码?

Is it possible to direct its output both to the console and into a log file in real-time without modifying the existing Java code?

如果Windows 7无法做到这一点,是否可以用Java编写Tee实用程序?

If Windows 7 is unable to do that, is it possible to write a Tee utility in Java?

它是否在Windows 8中解决了?

Is it solved in Windows 8?

推荐答案

要做到这一点而不修改现有的Java代码你可以写另一个包装类,适当地重新分配 System.out ,然后调用现有的主类

To do it "without modifying the existing Java code" you could write another wrapper class that reassigns System.out appropriately and then calls the existing main class

package mypackage;
import java.io.*;
import org.apache.commons.io.output.*;

public class TeeWrapper {
  public static void main(String[] args) throws Exception {
    FileOutputStream logFile = new FileOutputStream("log.txt");
    try {
      System.setOut(new PrintStream(new TeeOutputStream(System.out, logFile)));
      MyProgram.main(args);
    } finally {
      logFile.close();
    }
  }
}

(使用 TeeOutputStream

你运行包装器而不是原始类

You run the wrapper instead of the original class

java -classpath classfolder mypackage.TeeWrapper

这篇关于是否可以在Windows批处理或Java中编写Tee?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆