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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。