Java從控制臺(tái)讀入數(shù)據(jù)的幾種方法總結(jié)
這里記錄Java中從控制臺(tái)讀入信息的幾種方式,已備后查!
(1)JDK 1.4(JDK 1.5和JDK 1.6也都兼容這種方法)
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(利用Scanner進(jìn)行讀?。?/strong>
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還可以很方便的掃描文件,讀取里面的信息并轉(zhuǎn)換成你要的類型,比如對“2 2.2 3.3 3.33 4.5 done”這樣的數(shù)據(jù)求和,見如下代碼:
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(利用java.io.Console進(jìn)行讀?。?/strong>
JDK6中提供了java.io.Console類專用來訪問基于字符的控制臺(tái)設(shè)備.
你的程序如果要與Windows下的cmd或者Linux下的Terminal交互,就可以用Console類代勞.(類似System.in和System.out)
但我們不總是能得到可用的Console, 一個(gè)JVM是否有可用的Console依賴于底層平臺(tái)和JVM如何被調(diào)用.
如果JVM是在交互式命令行(比如Windows的cmd)中啟動(dòng)的,并且輸入輸出沒有重定向到另外的地方,那么就可以得到一個(gè)可用的Console實(shí)例。
在使用 IDE 的情況下,是無法獲取到Console實(shí)例的,原因在于在 IDE 的環(huán)境下,重新定向了標(biāo)準(zhǔn)輸入和輸出流,也是就是將系統(tǒng)控制臺(tái)上的輸入輸出重定向到了 IDE 的控制臺(tái)中
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); } }
Console類還有個(gè)特色就是,專門對密碼(輸入無回顯)等安全字符,進(jìn)行了處理。專門提供 readPassword()方法,具體應(yīng)用見如下代碼:
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); // 使用后應(yīng)立即將數(shù)組清空,以減少其在內(nèi)存中占用的時(shí)間,增強(qiáng)安全性 password = null; System.exit(-1); } else { console.printf("Invalid username or password.\n"); } } } }
以上就是小編為大家?guī)淼腏ava從控制臺(tái)讀入數(shù)據(jù)的幾種方法總結(jié)的全部內(nèi)容了,希望對大家有所幫助,多多支持腳本之家~
相關(guān)文章
Java日期工具類時(shí)間校驗(yàn)實(shí)現(xiàn)
一般項(xiàng)目中需要對入?yún)⑦M(jìn)行校驗(yàn),比如必須是一個(gè)合法的日期,本文就來介紹一下Java日期工具類時(shí)間校驗(yàn)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12在IntelliJ IDEA 搭建springmvc項(xiàng)目配置debug的教程詳解
這篇文章主要介紹了在IntelliJ IDEA 搭建springmvc項(xiàng)目配置debug的教程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09SpringCloud?@RefreshScope刷新機(jī)制淺析
RefeshScope這個(gè)注解想必大家都用過,在微服務(wù)配置中心的場景下經(jīng)常出現(xiàn),他可以用來刷新Bean中的屬性配置,那大家對他的實(shí)現(xiàn)原理了解嗎?它為什么可以做到動(dòng)態(tài)刷新呢2023-03-03SpringBoot集成MyBatisPlus+MySQL的實(shí)現(xiàn)
MybatisPlus是國產(chǎn)的第三方插件, 它封裝了許多常用的CURDapi,免去了我們寫mapper.xml的重復(fù)勞動(dòng),本文主要介紹了SpringBoot集成MyBatisPlus+MySQL的實(shí)現(xiàn),感興趣的可以了解一下2023-10-10