Java.io.Console.flush() 方法
描述
java.io.Console.flush() 方法刷新控制台并强制立即写入任何输出。
声明
以下是 java.io.Console.flush() 方法的声明 −
public void flush()
参数
NA
返回值
此方法不返回任何值。
异常
NA
示例
下面的例子展示了 java.io.Console.flush() 方法的使用。
package com.tutorialspoint;
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();
}
}
}
Following is the input −
Master Programmer
让我们编译并运行上面的程序,这将产生下面的结果 −
You have entered : Master Programmer

