如下代码:
我在后台输入 1234567890
但是下面这段程序输入是:
1234567890
34567890
多了一段 34567890
import java.io.*;
public class Person {
public static void main(String args[]) {
int b =10;
byte[] x = new byte[b];
try {
System.out.println("please Input:");
while ((b = System.in.read(x, 0, b)) != -1 && (b>0)) {
String s = new String(x);
System.out.println(s);
}
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
以下是我的理解:
public static void main(String[] args){
int b =10;
byte[] x = new byte[b];
try {
System.out.println("please Input:");
/* while ((b = System.in.read(x, 0, b)) != -1 && (b>0)) {
String s = new String(x);
System.out.println(s);
}*/
//下面一行运行完后 s=1234567890 b=10 x=[49, 50, 51, 52, 53, 54, 55, 56, 57, 48] 对应0-9的字节值
b = System.in.read(x, 0, b);
String s = new String(x);
System.out.println(s);
//下面一行运行完后x=[13, 10, 51, 52, 53, 54, 55, 56, 57, 48]因为刚才输入完0-9后又输入了回车符,对应输入了回车符和换行符,字节值为13和10
b = System.in.read(x, 0, b);
String s1 = new String(x);
System.out.println(s1);
//因为没有输入了,所以下面不会被运行
b = System.in.read(x, 0, b);
String s2 = new String(x);
System.out.println(s2);
} catch (IOException e) {
System.out.println(e.toString());
}
}
关于System.in.read方法的说明可参考http://blog.csdn.net/foart/article/details/8211327