2010-05-17 50 views
37

为了测试目的,我正在另一个应用程序中运行应用程序。我想将测试的应用程序的输出重定向到一个文件,所以每次测试后我都可以有一个日志。System.out到java中的文件

有没有办法将应用程序的输出重定向到java中命令行中的文件?

+2

它最有可能将是相对于Java虚拟机外部的解决方案,如在bash/sh的文件描述符重定向(如“app.exe> file.log 2>&1”),除非你使用了一些可配置的日志库 – bobah 2010-05-17 17:31:45

回答

64

您可以使用Windows命令行* nix shell支持的输出流重定向器,例如,

java -jar myjar.jar > output.txt 

另外,因为你是从VM内运行的应用程序,你可以从Java本身重定向System.out。您可以使用该方法

System.setOut(PrintStream ps)

它取代了标准输出流,所以到System.out所有后续调用到您指定的流。您可以在运行包装的应用程序之前执行此操作调用System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("output.txt"))));

如果您使用的是不能修改的包装,那么创建自己的包装。所以你有FEST包装 - >流重定向包装 - >测试应用程序。

例如,你可以实现一个简单的包装是这样的:

public class OutputRedirector 
{ 
    /* args[0] - class to launch, args[1]/args[2] file to direct System.out/System.err to */ 
    public static void main(String[] args) throws Exception 
    { // error checking omitted for brevity 
     System.setOut(outputFile(args(1)); 
     System.setErr(outputFile(args(2)); 
     Class app = Class.forName(args[0]); 
     Method main = app.getDeclaredMethod("main", new Class[] { (new String[1]).getClass()}); 
     String[] appArgs = new String[args.length-3]; 
     System.arraycopy(args, 3, appArgs, 0, appArgs.length); 
     main.invoke(null, appArgs); 
    } 
    protected PrintStream outputFile(String name) { 
     return new PrintStream(new BufferedOutputStream(new FileOutputStream(name)), true); 
    } 
} 

你有3个额外的PARAMS调用它 - 主要的类来运行,并且输出/错误指示。

+3

而且要清楚的是,这与java本身无关,而是shell提供的标准功能。 – 2010-05-17 17:31:19

+1

谢谢,我已经更新了我的答案,以表明这一点。 – mdma 2010-05-17 17:36:48

+0

我试过这样做,但我最终得到一个空的txt文件。输出仍然要去终端。 问题是我正在使用名为FEST(http://fest.easytesting.org)的测试库中的帮助类。这个帮助类接受它可以传递给应用程序的参数。流重定向器可能不太适合在这里 – Omar 2010-05-17 17:38:21

24

是的,你可以设置你想要的文件这样。

try { 
    System.setOut(new PrintStream(new File("output-file.txt"))); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

-1打印栈跟踪 – artbristol 2013-03-26 19:23:55

+0

@artbristol -1如果有空块。 – 2016-11-17 12:41:04

+0

我知道这是一个非常老的线程。有人可以解释'System.setOut(new PrintStream(新的File(“output-file.txt”)));''和'new PrintStream(new BufferedOutputStream(new FileOutputStream(“output-file.txt”)),true);'?为什么会选择另一个?如果我问的是天真的问题,但是我没有看到任何区别。我正在使用JDK8 – 2018-02-11 16:39:30

7

为了提高我使用的代码波纹管直接将文件或主目录在Linux中我的文档在Windows“vijay.shad”响应。

try { 
     System.setOut(new PrintStream(new File(FileSystemView.getFileSystemView() 
       .getDefaultDirectory().toString() 
       + File.separator + "output-file.txt"))); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
+0

-1来打印堆栈跟踪 – artbristol 2013-03-26 19:24:53

41

使用此构造:

new PrintStream(new BufferedOutputStream(new FileOutputStream("file.txt")));

记得设置autoflushing为真,即:

new PrintStream(new BufferedOutputStream(new FileOutputStream("file.txt")), true);

,否则你可能会得到空文件的程序结束后还是一样。

+1

好的抓人! – yhcowboy 2013-02-02 02:38:53

+0

@ben [There is a constructo r](https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#PrintStream(java.io.OutputStream,%20boolean))在PrintStream上支持自动刷新。 – heenenee 2016-06-02 00:01:09

+0

@heenenee我明白了。然而,我的评论是指在我发表评论后修改的部分答案。另请参阅答案的编辑历史记录。 – ben 2016-06-02 15:34:53

3

System.out.println()用于在控制台上打印消息。

系统是在java.lang包中定义的一个类。 out是PrintStream的一个实例,它是类System的公共和静态成员。因为PrintStream类的所有实例都有一个公共方法println()。

System.out是一个静态PrintStream写入控制台。我们可以使用将PrintStream作为参数的方法System.setOut()将输出重定向到不同的PrintStream。

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.PrintStream; 

public class SetPrintStream { 
    public static void main(String[] args) throws FileNotFoundException{ 
      System.out.println("Print on console"); 

      // Store console print stream. 
      PrintStream ps_console = System.out; 

      File file = new File("file.txt"); 
      FileOutputStream fos = new FileOutputStream(file); 

      // Create new print stream for file. 
      PrintStream ps = new PrintStream(fos); 

      // Set file print stream. 
      System.setOut(ps); 
      System.out.println("Print in the file !!"); 

      // Set console print stream. 
      System.setOut(ps_console); 
      System.out.println("Console again !!"); 
} 
} 

Output: 
Print on console 
Console again !! 
new file.txt will be created. 

欲了解更多信息,请参见我的博客:

http://javaexplorer03.blogspot.in/2016/02/how-do-i-redirect-standard-output-to.html

1
File file = new File("xyz.txt");   
    PrintStream printStreamToFile = new PrintStream(file); 
    System.setOut(printStreamToFile); 
    System.out.println("Hello I am writing to File xyz.txt");