2010-05-17 36 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");