Here we record several methods of reading information from the console in Java, which are standby for future query!
(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 using iterator)
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(); }}
The Scanner can also easily scan the file, read the information in it, and convert it to the type you want. For example, to sum the data like "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 (read using java. io. Console)
JDK 6 provides java. io. Console class for accessing character-based Console devices.
If your program needs to interact with cmd in Windows or Terminal in Linux, you can use the Console class to do the work. (such as System. in And System. out)
However, we cannot always get the 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 in interactive command lines (such as Windows cmd) and the input and output are not redirected to another place, an available Console instance can be obtained.
When IDE is used, the Console instance cannot be obtained because the standard input and output streams are redirected in the IDE environment, that is, redirect the input and output from the system console 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 characteristic of the Console class is that it processes security characters such as passwords (no echo input. The readPassword () method is provided specially. For specific applications, see the following code:
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); // after use, the array should be cleared immediately to reduce the time it occupies in the memory. Enhanced Security password = null; System. exit (-1);} else {console. printf ("Invalid username or password. \ n ");}}}}
References:
(1) full introduction to reading data from the console using Java
(2) Use java. io. Console to interact with the character Console