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()每次只是读取一个字符
 * 按下回车键代表了两个字符\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

猜你喜欢

转载自www.cnblogs.com/wjlwo2ni/p/10551351.html
今日推荐