九九热这里直有精品,1区二区三区在线播放,玖玖爱在线观看资源,国产aⅴ综合网,午夜福利男女,日本亚洲欧美三级,日韩无码黄色导航,内射少妇13区,中文字幕高清网

您身邊的軟件定制專(zhuān)家--9年開(kāi)發(fā)經(jīng)驗(yàn)為您護(hù)航

18678812288
0531-88887250

Java 字符終端上獲取輸入三種方式

文章作者:濟(jì)南軟件開(kāi)發(fā) 時(shí)間:2016年09月27日

  山東軟件開(kāi)發(fā)在Java 字符終端上獲取輸入有三種方式:

  1、java.lang.System.in (目前JDK版本均支持)

  2、java.util.Scanner (JDK版本>=1.5)

  3、java.io.Console(JDK版本>=1.6),特色:能不回顯密碼字符

  參考:

  這里記錄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)行讀取)

  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)換成你要的類(lèi)型,比如對(duì)“2 2.2 3.3 3.33 4.5 done”這樣的數(shù)據(jù)求和,見(jiàn)如下代碼:

  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)行讀?。?/span>

  JDK6中提供了java.io.Console類(lèi)專(zhuān)用來(lái)訪問(wèn)基于字符的控制臺(tái)設(shè)備.

  你的程序如果要與Windows下的cmd或者Linux下的Terminal交互,就可以用Console類(lèi)代勞.(類(lèi)似System.in和System.out)

  但我們不總是能得到可用的Console, 一個(gè)JVM是否有可用的Console依賴(lài)于底層平臺(tái)和JVM如何被調(diào)用.

  如果JVM是在交互式命令行(比如Windows的cmd)中啟動(dòng)的,并且輸入輸出沒(méi)有重定向到另外的地方,那么就可以得到一個(gè)可用的Console實(shí)例。

  在使用 IDE 的情況下,是無(wú)法獲取到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類(lèi)還有個(gè)特色就是,專(zhuān)門(mén)對(duì)密碼(輸入無(wú)回顯)等安全字符,進(jìn)行了處理。專(zhuān)門(mén)提供 readPassword()方法,具體應(yīng)用見(jiàn)如下代碼:

  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");

  }

  }

  }

  }


想要了解更多詳情歡迎來(lái)電咨詢(xún)18678812288
登陸網(wǎng)址:m.h6244.cn。
聯(lián)系人:王經(jīng)理。

余干县| 阿巴嘎旗| 浮梁县| 赞皇县| 西吉县| 阿瓦提县| 津南区| 高要市| 葫芦岛市| 黑山县| 萨迦县| 灵武市| 临桂县| 梅州市| 温宿县| 大兴区| 吉木萨尔县| 德兴市| 沂源县| 濮阳市| 翁源县| 武隆县| 荆门市| 桦川县| 扶余县| 宝丰县| 雷山县| 兴业县| 嘉善县| 漯河市| 龙陵县| 台江县| 蓬莱市| 张家川| 晋州市| 银川市| 成都市| 五河县| 武穴市| 青川县| 阿尔山市|