system.setout_Java System类setOut()方法及示例
system.setout
系統類setOut()方法 (System class setOut() method)
setOut() method is available in java.lang package.
setOut()方法在java.lang包中可用。
setOut() method is used to assign again the standard output stream or in other words, this method is used to reassign the standard output stream.
setOut()方法用于再次分配標準輸出流,或者換句話說,該方法用于重新分配標準輸出流。
The setOut() method is redirected because it does not write the output on the editor.
setOut()方法被重定向,因為它沒有在編輯器上寫入輸出。
The setOut() method does not write the output to the proper JTextArea like setErr() method and it returns null.
setOut()方法不會像setErr()方法那樣將輸出寫入適當的JTextArea,并且它返回null。
This is a static method, so this method is accessible with the class name too.
這是一個靜態方法,因此也可以使用類名訪問此方法。
setOut() method may throw an exception while writing the output to the standard output stream and the description is given below
setOut()方法在將輸出寫入標準輸出流時可能會引發異常,下面給出了描述
SecurityException: In this exception, the checkPermission() method does not allow reassigning of the latest standard output stream when the security manager exists.
SecurityException:在這種例外情況下,當安全管理器存在時, checkPermission()方法不允許重新分配最新的標準輸出流。
Syntax:
句法:
public static void setOut(PrintStream set_out);Parameter(s):
參數:
set_out – represents the latest standard output stream.
set_out –代表最新的標準輸出流。
Return value:
返回值:
The return type of this method is void, it returns nothing
此方法的返回類型為void ,不返回任何內容
Example:
例:
// Java program to demonstrate the example of // setOut() method of System Class.import java.lang.*; import java.io.*;public class SetOutMethod {public static void main(String[] args) throws Exception {// Creating an instance of FileFile file = new File("E://Programs//getProperties().doc");// By using exists() method returns true if file existsSystem.out.println("file existing is = " + file.exists());// Creating an instance of FileOutputStream fosFileOutputStream fos = new FileOutputStream(file);System.setOut(new PrintStream(fos));System.out.println("File Writing Done!!");} }Output
輸出量
E:\Programs>javac SetOutMethod.java E:\Programs>java SetOutMethod file existing is = trueOutput – if file does not exist
輸出–如果文件不存在
E:\Programs>javac SetOutMethod.java E:\Programs>java SetOutMethodException in thread "main" java.io.FileNotFoundException: E:/Programs/getProperties().doc (No such file or directory)at java.base/java.io.FileOutputStream.open0(Native Method)at java.base/java.io.FileOutputStream.open(FileOutputStream.java:299)at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:238)at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:188)at SetOutMethod.main(SetOutMethod.java:16)翻譯自: https://www.includehelp.com/java/system-class-setout-method-with-example.aspx
system.setout
總結
以上是生活随笔為你收集整理的system.setout_Java System类setOut()方法及示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 社会实践分组(c++)
- 下一篇: Java mybatis-plus详解