當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.lang.System.setErr()用法及代碼示例


描述

這個java.lang.System.setErr() 方法重新分配 "standard" 錯誤輸出流。

聲明

以下是聲明java.lang.System.setErr()方法

public static void setErr(PrintStream err)

參數

err- 這是新的標準錯誤輸出流。

返回值

此方法不返回任何值。

異常

SecurityException- 如果安全管理器存在並且其 checkPermission 方法不允許重新分配標準錯誤輸出流。

示例

下麵的例子展示了 java.lang.System.setErr() 方法的用法。

package com.tutorialspoint;

import java.lang.*;

public class SystemDemo {

   public static void main(String[] args) throws Exception {

      // create a file
      FileOutputStream f = new FileOutputStream("file.txt");
 
      System.setErr(new PrintStream(f));
      
      // redirect the output
      System.err.println("This will get redirected to file");
   }
}

假設我們有一個文本文件file.txt它作為我們示例程序的輸出生成。文件內容包括 -

This will get redirected to file

相關用法


注:本文由純淨天空篩選整理自 Java.lang.System.setErr() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。