Java Console writer()方法
java.io.Console.writer() 用于检索与控制台关联的PrintWriter对象。
1 语法
public PrintWriter writer()
2 参数
无
3 返回值
返回与Console关联的Writer。
4 示例
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.io.Console.writer()法的例子
*/
import java.io.Console;
import java.io.PrintWriter;
public class Demo {
public static void main(String[] args) {
Console cnsl = null;
PrintWriter out = null;
try {
// creates a console object
cnsl = System.console();
// if console is not null
if (cnsl != null) {
// creates new print writer
out = cnsl.writer();
// prints
out.println("Here is The Optimus Prime!!");
}
} catch(Exception ex) {
// if any error occurs
ex.printStackTrace();
}
}
}
输出结果为:
Here is The Optimus Prime!!