国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java從控制臺讀入數據的幾種方法總結

Java從控制臺讀入數據的幾種方法總結

2020-06-20 12:29jingxian JAVA教程

下面小編就為大家帶來一篇Java從控制臺讀入數據的幾種方法總結。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

這里記錄Java中從控制臺讀入信息的幾種方式,已備后查!

(1)JDK 1.4(JDK 1.5和JDK 1.6也都兼容這種方法)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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進行讀取)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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還可以很方便的掃描文件,讀取里面的信息并轉換成你要的類型,比如對“2 2.2 3.3 3.33 4.5 done”這樣的數據求和,見如下代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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進行讀取)

JDK6中提供了java.io.Console類專用來訪問基于字符的控制臺設備.

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

但我們不總是能得到可用的Console, 一個JVM是否有可用的Console依賴于底層平臺和JVM如何被調用.

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

在使用 IDE 的情況下,是無法獲取到Console實例的,原因在于在 IDE 的環境下,重新定向了標準輸入和輸出流,也是就是將系統控制臺上的輸入輸出重定向到了 IDE 的控制臺中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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類還有個特色就是,專門對密碼(輸入無回顯)等安全字符,進行了處理。專門提供 readPassword()方法,具體應用見如下代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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);
       // 使用后應立即將數組清空,以減少其在內存中占用的時間,增強安全性
        password = null;
       System.exit(-1);
      }
      else {
       console.printf("Invalid username or password.\n");
      }
      }
     }
 
}

以上就是小編為大家帶來的Java從控制臺讀入數據的幾種方法總結的全部內容了,希望對大家有所幫助,多多支持服務器之家~

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美福利在线观看 | 欧美国产日韩一区二区三区 | 在线观看国产一区二区 | 亚洲视频在线播放 | 亚洲免费美女视频 | 国产精品日韩在线观看 | 午夜欧美 | 日本 欧美 国产 | 狠狠人人 | 二区视频 | 亚洲视频 欧美视频 | 91精品国产综合久久久久久丝袜 | 黄色在线免费 | 久久久久国产精品www | 伊人色私人影院蜜桃va | 毛片在线视频 | 亚洲国产一区二区在线观看 | 亚洲精品日韩精品 | 玖玖精品在线 | 国内精品久久久久久中文字幕 | 在线a视频 | 久久99精品一区二区三区三区 | 亚洲精品日韩精品 | 欧美国产精品一区 | 欧美精品一区二区久久 | 免费观看爱爱视频 | 欧美中文在线 | 91国内外精品自在线播放 | 国产99久久精品 | 亚洲伦理一区 | 91精品一区二区三区久久久久久 | 99久久国产露脸国语对白 | 亚洲毛片网站 | 中文字幕av网站 | 欧美精品亚洲精品 | 在线中文字幕第一页 | 久久久久久网站 | 羞羞的网站 | 免费看黄色一级电影 | 天天躁人人躁人人躁狂躁 | 一区影院 |