Java中控製台類的writer()方法用於檢索與控製台關聯的唯一PrintWriter對象。
用法:
public PrintWriter writer()
參數:此方法不接受任何參數。
返回值:此方法返回與控製台關聯的PrintWriter。
異常:此方法沒有任何拋出異常。
注意:System.console()在在線IDE中返回null。
以下示例程序旨在說明IO包中Console類中的writer()方法:
程序1:
// Java program to illustrate
// Console writer() method
import java.io.*;
public class GFG {
public static void main(String[] args)
{
// Create the console object
Console cnsl
= System.console();
if (cnsl == null) {
System.out.println(
"No console available");
return;
}
// Create PrintWriter
PrintWriter pw = cnsl.writer();
System.out.println(
"PrintWriter is created and returned");
}
}
輸出:
程序2:
// Java program to illustrate
// Console writer() method
import java.io.*;
public class GFG {
public static void main(String[] args)
{
// Create the console object
Console cnsl
= System.console();
if (cnsl == null) {
System.out.println(
"No console available");
return;
}
// Create PrintWriter
PrintWriter pw = cnsl.writer();
System.out.println(
"PrintWriter is created and returned");
}
}
輸出:
參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/io/Console.html#writer()
相關用法
- Java Writer write(int)用法及代碼示例
- Java Writer close()用法及代碼示例
- Java Writer toString()用法及代碼示例
- Java Writer flush()用法及代碼示例
- Java Writer equals()用法及代碼示例
- Java Writer getClass()用法及代碼示例
- Java Writer append(CharSequence, int, int)用法及代碼示例
- Java Writer write(String, int, int)用法及代碼示例
- Java Writer write(String)用法及代碼示例
- Java Writer write(char[], int, int)用法及代碼示例
- Java Writer append(char)用法及代碼示例
- Java Writer write(char[])用法及代碼示例
- Java Writer append(CharSequence)用法及代碼示例
- Java Console flush()用法及代碼示例
- Java Console reader()用法及代碼示例
- Java Console readPassword()用法及代碼示例
- Java Console readLine()用法及代碼示例
- Java Console printf(String, Object)用法及代碼示例
- Java Console format(String, Object)用法及代碼示例
- Java Writer hashCode()用法及代碼示例
注:本文由純淨天空篩選整理自pp_pankaj大神的英文原創作品 Console writer() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。