Here are several ways to read information from the console in Java, and check it later!
(1) JDK 1.4 (JDK 1.5 and JDK 1.6 are also compatible with this method)
public class TestConsole1 { public static void main(String[] args) { String str = readDataFromConsole("Please input string:); System.out.println("The information from console: + str); } /** * Use InputStreamReader and System.in to read data from console * * @param prompt * * @return input string */ private static String readDataFromConsole(String prompt) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = null; try { System.out.print(prompt); str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; }}(2) JDK 1.5 (read with Scanner)
public class TestConsole2 { public static void main(String[] args) { String str = readDataFromConsole("Please input string:"); System.out.println("The information from console:" + str); } /** * Use java.util.Scanner to read data from console * * @param prompt * * @return input string */ private static String readDataFromConsole(String prompt) { Scanner scanner = new Scanner(System.in); System.out.print(prompt); return scanner.nextLine(); }}Scanner can also easily scan files, read the information inside and convert it to the type you want, for example, summing data such as "2 2.2 3.3 3.33 4.5 done", see the following code:
public class TestConsole4 { public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("num.txt"); fw.write("2 2.2 3.3 3.33 4.5 done"); fw.close(); System.out.println("Sum is "+scanFileForSum("num.txt")); } public static double scanFileForSum(String fileName) throws IOException { double sum = 0.0; FileReader fr = null; try { fr = new FileReader(fileName); Scanner scanner = new Scanner(fr); while (scanner.hasNext()) { if (scanner.hasNextDouble()) { sum = sum + scanner.nextDouble(); } else { String str = scanner.next(); if (str.equals("done")) { break; } else { throw new RuntimeException("File Format is wrong!"); } } } } catch (FileNotFoundException e) { throw new RuntimeException("File " + fileName + " not found!"); } finally { if (fr != null) fr.close(); } return sum; }}(3) JDK 1.6 (use java.io.Console for reading)
JDK6 provides a java.io.Console class specifically for 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 to do it. (Similar to System.in and System.out)
But we don't always get the available Console, whether a JVM has available Console depends on the underlying platform and how the JVM is called.
If the JVM is started on an interactive command line (such as Windows cmd) and the input and output are not redirected to another place, then you can get an available Console instance.
When using the IDE, the Console instance cannot be obtained. The reason is that in the IDE environment, the standard input and output streams are redirected, which means that the input and output on the system console are redirected to the IDE console
public class TestConsole3 { public static void main(String[] args) { String str = readDataFromConsole("Please input string:"); System.out.println("The information from console:" + str); } /** * Use java.io.console to read data from console * * @param prompt * * @return input string */ private static String readDataFromConsole(String prompt) { Console console = System.console(); if (console == null) { throw new IllegalStateException("Console is not available!"); } return console.readLine(prompt); }}Another feature of the Console class is that it specifically processes security characters such as passwords (without echo input). Specially provide the readPassword() method, see the following code for the specific application:
public class TestConsole5 { public static void main(String[] args) { Console console = System.console(); if (console == null) { throw new IllegalStateException("Console is not available!"); } while(true){ String username = console.readLine("Username: "); char[] password = console.readPassword("Password: "); if (username.equals("Chris") && String.valueOf(password).equals("GoHead")) { console.printf("Welcome to Java Application %1$s./n", username); // The array should be cleared immediately after use to reduce its time in memory and enhance security password = null; System.exit(-1); } else { console.printf("Invalid username or password./n"); } } } }}The above is the summary of several ways to read data from the console by the editor. I hope it will be helpful to everyone and support Wulin.com more~