天天看點

JAVA中的System.in - 江清瀾靜

JAVA中的System.in

System.in讀取标準輸入裝置資料(從标準輸入擷取資料,一般是鍵盤),其資料類型為InputStream。方法:

  int read()   // 傳回輸入數值的ASCII碼,,該值為0到 255範圍内的int位元組值。若傳回值為-1,說明沒有讀取到任何位元組讀取工作結束。

  int read(byte[] b)  // 讀入多個位元組到緩沖區b中,傳回值是讀入的位元組數

1 package InPackage;
 2 
 3 /**
 4  * System.in.read()傳回值為輸入數值的ASCII碼,該值為0到 255範圍内的int位元組值
 5  * 如果因為已經到達流末尾而沒有可用的位元組,則傳回值 -1。
 6  */
 7 public class Intest1 {
 8     public static void main(String args[]) throws java.io.IOException
 9       {
10        int a=0;
11        System.out.println("請輸入a:");
12        a=System.in.read();
13        System.out.println("a="+a);
14        System.out.println("(char)a="+(char)a);
15       }
16     /**
17      * 假設我們輸入a為1
18      * 輸出結果為:
19      * 請輸入a:
20      * 1
21      * a=49
22      * (char)a=1
23      */
24           

有一個有意思的問題是:當我們輸入一個字元,System.in.read()會讀取幾個字元呢?

1 package InPackage;
 2 
 3 import java.util.Arrays;
 4 
 5 /**
 6  * 當我們輸入一個字元,System.in.read()會讀取幾個字元
 7  * 我們從運作結果可以看出是三個
 8  * 假設我們輸入一個字元,那麼它會接着讀取該字元後面的/r和/n
 9  */
10 public class Intest2 {
11      public static void main(String[] args) throws Exception {  
12          int[] x = new int[6];  
13          Arrays.fill(x, 5);  //Arrays.fill(int[] a,int b)方法用于給數組中的每個元素指派
14          for (int i = 0; i < x.length; i++) {  
15              System.in.read();  
16              System.out.println(x[i]);  
17          }  
18      }
19      /**
20       * 假設我們輸入值分别為1,2
21       * 輸出結果:
22       * 1
23       * 5
24       * 5
25       * 5
26       * 2
27       * 5
28       * 5
29       * 5
30       */
31 }      

System.in.read()每次隻是讀取一個字元,但它多讀取的是哪幾個字元呢?

import java.io.IOException;

/**
 * System.in.read()每次隻是讀取一個字元
 * 按下Enter鍵代表了兩個字元\r\n,\r的ASCII碼值是10,\n是13。另外,1對應的ASCII是49
 */

public class Intest3 {
    public static void main(String args[]) throws IOException {
        for (int j = 0; j < 5; j++) {
            System.out.println("請輸入:");
            char c = 0;
            c = (char) System.in.read();
            if (c == \'1\') {
                System.out.println("OK!");
            } else {
                System.out.println((int) c);
            }
        }
    }
}      

對于上面的程式,我們首先輸入的是w1,結果如下圖所示:

JAVA中的System.in - 江清瀾靜

  可以看出程式還沒有執行完,阻塞于最後一個“請輸入:”,此時我們再次輸入1,程式執行完成,結果如下圖所示:

JAVA中的System.in - 江清瀾靜

如何讓System..in.read()讀入一行資料呢?

1 package InPackage;
 2 
 3 import java.io.IOException;
 4 
 5 public class Intest4 {
 6     public static void main(String args[]) {
 7         int b;
 8         try {
 9             System.out.println("請輸入:");
10             while ((b = System.in.read()) != -1) {
11                 System.out.print((char) b);
12             }
13         } catch (IOException e) {
14             System.out.println(e.toString());
15         }
16     }
17     /**
18      * 輸出結果:
19      * 請輸入:
20      * test
21      * test
22      */
23 }      
1 package InPackage;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.DataInputStream;
 5 import java.io.InputStreamReader;
 6 
 7 /**
 8  * 通常情況下,你會用readLine( )一行一行地讀取輸入,
 9  * 是以要把System.in包裝成BufferedReader。但在這之前還得先用InputSteamReader把System.in轉換成Reader。
10  * BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
11  * in.readLine()傳回值為String類型
12  *
13  */
14 public class Intest5 {
15     public static void main(String args[]) throws java.io.IOException {
16         System.out.println("請輸入整數:");
17         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
18         //或者這麼寫也可以:DataInputStream reader = new DataInputStream(System.in);
19         int a = Integer.parseInt(reader.readLine()); // 這樣得到的是String類型的,需要轉換為需要的類型
20         System.out.println("a=" + a);
21         int sum = 0;
22         for (int i = 0; i <= a; i++)
23             sum += i;
24         System.out.println(sum);
25     }
26     /**
27      * 假設我們輸入a為100
28      * 輸出結果為:
29      * 100
30      * a=100
31      * 5050
32      */
33 }      

public int read(byte[] b) throws IOException又是怎麼使用的呢?

1 package InPackage;
 2 
 3 /**
 4  * public int read(byte[] b) throws IOException 
 5  * 從輸入流中讀取一定數量的位元組,并将其存儲在緩沖區數組 b中。
 6  * 傳回值為:以整數形式傳回實際讀取的位元組數。 
 7  * 如果 b的長度為0,則不讀取任何位元組并傳回 0; 否則,嘗試讀取至少一個位元組。 
 8  * 如果因為流位于檔案末尾而沒有可用的位元組,則傳回值 -1;否則,至少讀取一個位元組并将其存儲在b中。
 9  * 
10  */
11 public class Intest6 {
12     public static void main(String args[]) throws Exception {
13         byte[] barray = new byte[5];
14         System.out.println("請輸入:");
15         System.in.read(barray);
16         for (int i = 0; i < barray.length; i++) {
17             System.out.println((char) barray[i]);
18         }
19     }
20 }      

轉載:https://www.cnblogs.com/ningvsban/p/3593817.html