例子
以下示例显示java.io.Console.flush()方法的用法。
package com.jc2182;
import java.io.Console;
public class ConsoleDemo {
public static void main(String[] args) {
Console cnsl = null;
try {
//Create a console object.
cnsl = System.console();
// test for console not null
if (cnsl != null) {
// read line from the console
String name = cnsl.readLine("Enter name : ");
// print
System.out.println("You have entered : " + name);
}
// flushes console and forces output to be written
cnsl.flush();
} catch(Exception ex) {
// if any error occurs
ex.printStackTrace();
}
}
}
以下是输入-
让我们编译并运行以上程序,这将产生以下结果-
You have entered : Master Programmer