Summary of several methods of reading data from Java console
Here are several ways to read information from the console in Java, which will be checked later!
(1) JDK 1.4 (JDK 1.5 and JDK 1.6 are also compatible with this method)
(2) JDK 1.5 (read with scanner)
Scanner can also easily scan files, read the information inside and convert it to the type you want, such as summing data such as "2.2 3.3 3.33 4.5 done". See the following code:
(3) JDK 1.6 (read with java.io.console)
Java. Net is provided in JDK6 io. The console class is dedicated to accessing character - based console devices
If your program wants to interact with CMD under windows or terminal under Linux, you can use the console class (similar to system. In and system. Out)
But we can't always get an available console. Whether a JVM has an available console depends on the underlying platform and how the JVM is called
If the JVM is started from an interactive command line (such as windows CMD) and the input and output are not redirected to another place, you can get an available console instance.
When using the IDE, the console instance cannot be obtained because the standard input and output streams are redirected in the IDE environment, that is, the input and output on the system console are redirected to the IDE console
Another feature of the console class is that it deals with security characters such as passwords (no echo after input). Readpassword() method is specially provided. See the following code for specific application:
The above is the summary of several methods of Java reading data from the console brought by Xiaobian. I hope it will be helpful to you and support a lot of programming tips~