欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java從控制臺(tái)讀入數(shù)據(jù)的幾種方法總結(jié)

 更新時(shí)間:2016年10月02日 09:40:52   投稿:jingxian  
下面小編就為大家?guī)硪黄狫ava從控制臺(tái)讀入數(shù)據(jù)的幾種方法總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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

    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
  • mybatis中的if-else及if嵌套使用方式

    mybatis中的if-else及if嵌套使用方式

    這篇文章主要介紹了mybatis中的if-else及if嵌套使用方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • springcloud注冊hostname或者ip的那些事

    springcloud注冊hostname或者ip的那些事

    Spring cloud是一個(gè)基于Spring Boot實(shí)現(xiàn)的服務(wù)治理工具包,在微服務(wù)架構(gòu)中用于管理和協(xié)調(diào)服務(wù)的。這篇文章主要介紹了springcloud注冊hostname或者ip,需要的朋友可以參考下
    2019-11-11
  • 在IntelliJ IDEA 搭建springmvc項(xiàng)目配置debug的教程詳解

    在IntelliJ IDEA 搭建springmvc項(xiàng)目配置debug的教程詳解

    這篇文章主要介紹了在IntelliJ IDEA 搭建springmvc項(xiàng)目配置debug的教程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • springCloud中的Sidecar多語言支持詳解

    springCloud中的Sidecar多語言支持詳解

    這篇文章主要介紹了springCloud中的Sidecar多語言支持詳解,Sidecar是將一組緊密結(jié)合的任務(wù)與主應(yīng)用程序共同放在一臺(tái)主機(jī)Host中,但會(huì)將它們部署在各自的進(jìn)程或容器中,需要的朋友可以參考下
    2024-01-01
  • SpringCloud?@RefreshScope刷新機(jī)制淺析

    SpringCloud?@RefreshScope刷新機(jī)制淺析

    RefeshScope這個(gè)注解想必大家都用過,在微服務(wù)配置中心的場景下經(jīng)常出現(xiàn),他可以用來刷新Bean中的屬性配置,那大家對他的實(shí)現(xiàn)原理了解嗎?它為什么可以做到動(dòng)態(tài)刷新呢
    2023-03-03
  • Mybatis?如何開啟控制臺(tái)打印sql語句

    Mybatis?如何開啟控制臺(tái)打印sql語句

    這篇文章主要介紹了Mybatis?如何開啟控制臺(tái)打印sql語句問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • SpringBoot集成MyBatisPlus+MySQL的實(shí)現(xiàn)

    SpringBoot集成MyBatisPlus+MySQL的實(shí)現(xiàn)

    MybatisPlus是國產(chǎn)的第三方插件, 它封裝了許多常用的CURDapi,免去了我們寫mapper.xml的重復(fù)勞動(dòng),本文主要介紹了SpringBoot集成MyBatisPlus+MySQL的實(shí)現(xiàn),感興趣的可以了解一下
    2023-10-10
  • SpringBoot中的@Import注解四種使用方式詳解

    SpringBoot中的@Import注解四種使用方式詳解

    這篇文章主要介紹了SpringBoot中的@Import注解四種使用方式詳解,@Import注解只可以標(biāo)注在類上,可以結(jié)合 @Configuration注解、ImportSelector、ImportBeanDefinitionRegistrar一起使用,也可以導(dǎo)入普通的類,需要的朋友可以參考下
    2023-12-12
  • 淺析Spring的JdbcTemplate方法

    淺析Spring的JdbcTemplate方法

    本篇淺析Spring的JdbcTemplate方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01

最新評論