JAVA中的System.in


  System.in讀取標准輸入設備數據(從標准輸入獲取數據,一般是鍵盤),其數據類型為InputStream。方法:

  int read()   // 返回輸入數值的ASCII碼,,該值為0到 255范圍內的int字節值。若返回值為-1,說明沒有讀取到任何字節讀取工作結束。

  int read(byte[] b)  // 讀入多個字節到緩沖區b中,返回值是讀入的字節數

package InPackage;

/**
 * System.in.read()返回值為輸入數值的ASCII碼,該值為0到 255范圍內的int字節值
 * 如果因為已經到達流末尾而沒有可用的字節,則返回值 -1。
 */
public class Intest1 {
    public static void main(String args[]) throws java.io.IOException
      {
       int a=0;
       System.out.println("請輸入a:");
       a=System.in.read();
       System.out.println("a="+a);
       System.out.println("(char)a="+(char)a);
      }
    /**
     * 假設我們輸入a為1
     * 輸出結果為:
     * 請輸入a:
     * 1
     * a=49
     * (char)a=1
     */
    
}
View Code

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

package InPackage;

import java.util.Arrays;

/**
 * 當我們輸入一個字符,System.in.read()會讀取幾個字符
 * 我們從運行結果可以看出是三個
 * 假設我們輸入一個字符,那么它會接着讀取該字符后面的/r和/n
 */
public class Intest2 {
     public static void main(String[] args) throws Exception {  
         int[] x = new int[6];  
         Arrays.fill(x, 5);  //Arrays.fill(int[] a,int b)方法用於給數組中的每個元素賦值
         for (int i = 0; i < x.length; i++) {  
             System.in.read();  
             System.out.println(x[i]);  
         }  
     }
     /**
      * 假設我們輸入值分別為1,2
      * 輸出結果:
      * 1
      * 5
      * 5
      * 5
      * 2
      * 5
      * 5
      * 5
      */
}
View Code

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

package InPackage;

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);
            }
        }
    }
}
View Code

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

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

  如何讓System..in.read()讀入一行數據呢?

package InPackage;

import java.io.IOException;

public class Intest4 {
    public static void main(String args[]) {
        int b;
        try {
            System.out.println("請輸入:");
            while ((b = System.in.read()) != -1) {
                System.out.print((char) b);
            }
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }
    /**
     * 輸出結果:
     * 請輸入:
     * test
     * test
     */
}
View Code
package InPackage;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;

/**
 * 通常情況下,你會用readLine( )一行一行地讀取輸入,
 * 因此要把System.in包裝成BufferedReader。但在這之前還得先用InputSteamReader把System.in轉換成Reader。
 * BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 * in.readLine()返回值為String類型
 *
 */
public class Intest5 {
    public static void main(String args[]) throws java.io.IOException {
        System.out.println("請輸入整數:");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        //或者這么寫也可以:DataInputStream reader = new DataInputStream(System.in);
        int a = Integer.parseInt(reader.readLine()); // 這樣得到的是String類型的,需要轉換為需要的類型
        System.out.println("a=" + a);
        int sum = 0;
        for (int i = 0; i <= a; i++)
            sum += i;
        System.out.println(sum);
    }
    /**
     * 假設我們輸入a為100
     * 輸出結果為:
     * 100
     * a=100
     * 5050
     */
}
View Code

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

package InPackage;

/**
 * public int read(byte[] b) throws IOException 
 * 從輸入流中讀取一定數量的字節,並將其存儲在緩沖區數組 b中。
 * 返回值為:以整數形式返回實際讀取的字節數。 
 * 如果 b的長度為0,則不讀取任何字節並返回 0; 否則,嘗試讀取至少一個字節。 
 * 如果因為流位於文件末尾而沒有可用的字節,則返回值 -1;否則,至少讀取一個字節並將其存儲在b中。
 * 
 */
public class Intest6 {
    public static void main(String args[]) throws Exception {
        byte[] barray = new byte[5];
        System.out.println("請輸入:");
        System.in.read(barray);
        for (int i = 0; i < barray.length; i++) {
            System.out.println((char) barray[i]);
        }
    }
}
View Code

 

參考資料:

http://uule.iteye.com/blog/1128243
http://blog.sina.com.cn/s/blog_78edf5db0100xr7a.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM