簡體   English   中英

System.out 到 java 中的文件

[英]System.out to a file in java

我正在從另一個應用程序內部運行一個應用程序以進行測試。 我想將被測試應用程序的輸出重定向到一個文件,這樣我就可以在每次測試后都有一個日志。

有沒有辦法將應用程序的輸出從 java 中的命令行重定向到文件?

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

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 個附加參數調用它 - 要運行的 Main 類,以及輸出/錯誤指示。

使用此構造函數時:

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

記得將 autoflushing 設置為 true,即:

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

否則即使在程序完成后,您也可能會得到空文件。

是的,您可以像這樣設置所需的文件。

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

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

System 是在 java.lang 包中定義的一個類。 out 是 PrintStream 的一個實例,它是類 System 的公共和靜態成員。 因為 PrintStream 類的所有實例都有一個公共方法 println()。

System.out是寫入控制台的靜態 PrintStream 我們可以使用System.setOut()方法將輸出重定向到不同的 PrintStream,該方法將 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

為了改進“vijay.shad”響應,我使用下面的代碼將文件定向到 Linux 中的主目錄或 Windows 中的 MyDocuments。

    try {
        System.setOut(new PrintStream(new File(FileSystemView.getFileSystemView()
                .getDefaultDirectory().toString()
                + File.separator + "output-file.txt")));
    } catch (Exception e) {
         e.printStackTrace();
    }
    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");

或者您可以使用Class FileWriter

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM