一、介绍
创建文件扫描器对象,System.in表示的是标准输入,可以从控制台读取数据(装饰者模式),System.out表示标准输出。
System.in读取标准输入设备数据(从标准输入获取数据,一般是键盘),其数据类型为InputStream,方法为:
int read() -----返回输入数值的ASCII码,该值为0到255范围内的int字节值,若返回值为-1,则表示没有读取到任何字节,读取结束。
int read(byte[] b) -----读入多个字节到缓冲区b中,返回值是读如的字节数
1、举例如下:
import java.io.IOException;
public class Intest1 {
public static void main(String[] args) throws IOException {
int a = 0;
System.out.println("请输入a:");//输入0,从控制台接收输入,输入的是一个字符0,而不是数字0,
// 它的ASCII码值为48,0-9的ASCII码值依次为48-57
a = System.in.read();
System.out.println("a=" + a); //所以这里输出的是48
System.out.println("(char)a="+(char)a);
}
}
运行结果为:
2、然后我们来思考一个问题,当我们输入一个字符,System.in.read()会读取几个字符?
import java.util.Arrays;
public class Intest2 {
public static void main(String[] args) throws Exception {
int[] x = new int[3];
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]);
}
}
}
输出结果为:
从运行结果来看,当我们输入一个字符,System.in.read()读取了2个字符 ,那么它是怎么读取的呢?当输入一个字符,按下回车的时候,代表了2个字符\r\n,\r的ASCII码值是10,\n是13。继续看下面:
3、
import java.io.IOException;
public class Intest3 {
public static void main(String args[]) throws IOException {
/**
* System.in.read()每次只是读取一个字符,当输入1时,按下回车键代表了1个字符\r,
* \r的ASCII码值是10,所以输入1后,程序2次循环读取2次,第一次读取输入的1,打印输出OK,
* 第2次读取\r的ASCII码值10,转换为字符10,打印输出它的整型数据10
*/
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);
}
}
}
}
运行如下:
4、System.in.read()读取数据不结束
System.in.read()读取,返回的是数值的ASCII码,该值为0到 255范围内的int字节值。若返回值为-1,说明没有读取到任何字节,读取工作结束,一行读取完成
import java.io.IOException;
public class Intest4 {
public static void main(String args[]) throws IOException {
int b;
System.out.println("请输入:");
while ((b = System.in.read()) != -1) {
System.out.print((char) b);
}
}
}
运行结果如下:
5、readLine()读取一行
通常情况下,你会用readLine( )一行一行地读取输入,因此要把System.in包装成BufferedReader。但在这之前还得先用InputSteamReader把System.in转换成Reader。 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); in.readLine()返回值为String类型
import java.io.BufferedReader;
import java.io.InputStreamReader;
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);
}
}
运行结果:假设输入数据10,最终求和结果为55。
6、int read(byte[] b) 读取数据
public class Intest6 {
public static void main(String args[]) throws Exception {
/**
* public int read(byte[] b) throws IOException
* 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b中。
* 返回值为:以整数形式返回实际读取的字节数。
* 如果 b的长度为0,则不读取任何字节并返回 0; 否则,尝试读取至少一个字节。
* 如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少读取一个字节并将其存储在b中。
*
*/
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]);
}
}
}
运行结果为:
我们输入数据1和 2,把1和2存储在缓冲区数组b中
参考文档:
https://blog.csdn.net/weixin_33985507/article/details/85610919