1. 程式人生 > >JAVA中的System.in

JAVA中的System.in

oid 說明 嘗試 line mage [] main buffered int

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()每次只是讀取一個字符
 * 按下回車鍵代表了兩個字符\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,結果如下圖所示:

技術分享圖片

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

技術分享圖片

如何讓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

JAVA中的System.in