例子
以下示例显示java.io.Console.writer()方法的用法。
package com.jc2182;
import java.io.Console;
import java.io.PrintWriter;
public class ConsoleDemo {
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!!