java程序中加入下面这段代码,神奇的事情就发生了:
Scanner sc = new Scanner(System.in);
try{
System.out.println("名字: ");
String str = sc.next();
System.out.println("用户输入" + str);
}catch(Exception e){
e.printStackTrace();
}
我们就可以从键盘输入一段字符串被系统接收了。
出于好奇,看了下源代码,发现最为关键的地方是它调用了System.in里面的read方法。
System.in
in
public static final InputStream in“标准”输入流。此流已打开并准备提供输入数据。通常,此流对应于键盘输入或者由主机环境或用户指定的另一个输入源。
模拟了下实现过程:
private static char[] lineBuffer;
/**
* read
* @throws IOException
*/
public static void read() throws IOException {
InputStream in = System.in;
char[] buf = lineBuffer;
if (buf == null) {
buf = lineBuffer = new char[128];
}
int room = buf.length;
int offset = 0;
int c;
while (true) {
c = in.read();
if (--room < 0) {
buf = new char[offset + 128];
room = buf.length - offset - 1;
System.arraycopy(lineBuffer, 0, buf, 0, offset);
lineBuffer = buf;
}
buf[offset++] = (char) c;
}
//return String.copyValueOf(buf, 0, offset);
}
public static String readLine() throws IOException {
InputStream in = System.in;
char[] buf = lineBuffer;
if (buf == null) {
buf = lineBuffer = new char[128];
}
int room = buf.length;
int offset = 0;
int c;
loop: while (true) {
switch (c = in.read()) {
case -1:
case '\n':
break loop;
case '\r':
int c2 = in.read();
if ((c2 != '\n') && (c2 != -1)) {
if (!(in instanceof PushbackInputStream)) {
in = new PushbackInputStream(in);
}
((PushbackInputStream) in).unread(c2);
}
break loop;
default:
if (--room < 0) {
buf = new char[offset + 128];
room = buf.length - offset - 1;
System.arraycopy(lineBuffer, 0, buf, 0, offset);
lineBuffer = buf;
}
buf[offset++] = (char) c;
break;
}
}
if ((c == -1) && (offset == 0)) {
return null;
}
return String.copyValueOf(buf, 0, offset);
}
调用我写的read方法,可以一直不停的输入。
调用readLine()方法,当有换行的时候,就停止了。
当然,java.io.BufferedReader的构造器也可以传入一个System.in
BufferedReader bReader = null;
try{
System.out.println("名字: ");
bReader = new BufferedReader(new InputStreamReader(System.in));
String str = bReader.readLine();
System.out.println("用户输入" + str);
}catch(Exception e){
e.printStackTrace();
}
我们也可以从键盘进行输入,然后读取一下,效果和Scanner是一样 的。
现在在看这段代码,倍感亲切了!
分享到:
相关推荐
直流电源系统的常见故障是一点接地,在一般情况下一点接地并不影响直流系统的运行,但如果不能迅速找到接地故障点并予以修复,又发生另一点接地故障就可能会发生最大事故,所以对直流系统绝缘状况进行实时监测,出现...
”因为网上好像并没有一个比较完整的教材,所以抽一点时间写了这片《简明批处理教程》给新手朋友们.也献给所有为实现网络的自由与共享而努力的朋友们. 批处理文件是无格式的文本文件,它包含一条或多条命令。它的...
这一点是非常必要的,因为一个基于趋势的交易系统是不能成功交易在盘整(sideways trends),也不能识别市场的回调(setbacks)和逆转(reversals.,反向走势)!当然,你可以采用两个ATS,一个基于“趋势”,一个基于“反...
Now system is in safe mode. Please re-setting CPU frequency in CMOS setup. 警告:现在系统处在安全模式,从新进入CMOS 进行设置。 CMOS checksun error-del anlts loaded 或是CMOS检查出错。 出现上面两种错...
复制相应 *.CTL 文件到软件安装目录,重新启动 EditPlus ,则系统自动识别。 作者主页有很多语法自动完成文件下载,地址 http://editplus.com/files.html 【14】工具集成——编译器集成例子(Java、Borland C++、...
复制相应 *.CTL 文件到软件安装目录,重新启动 EditPlus ,则系统自动识别。 上面重复使用了“[0-9]”,表示连续出现的三个数字 “\0”代表第一个“[0-9]”对应的原型,“\1”代表第二个“[0-9]”对应的原型,...
复制相应 *.CTL 文件到软件安装目录,重新启动 EditPlus ,则系统自动识别。 作者主页有很多语法自动完成文件下载,地址 http://editplus.com/files.html 【14】工具集成——编译器集成例子(Java、Borland C++、...
我对J2EE和.NET的一点理解 难分难舍的DSO(一) InternalsVisibleToAttribute,友元程序集访问属性 Essential .NET 读书笔记 [第一部分] NET FrameWork的Collections支持 .NET的反射在软件设计上的应用 关于跨程序集...
1.Copy the driver (filedisk.sys) to %systemroot%\system32\drivers\. 2.Import filedisk.reg to the Registry. 3.Reboot. 4.Use the program filedisk.exe to mount/umount files, for an example of use ...
6 pSysHead->system_flag = 0; 7 head_sum(); 8 continue; 9 } 10 if(strlen(dbf->dbf_name) != 0 && strncmp(dbf->dbf_name, pDBFat->dbf_name, strlen(dbf->dbf_name)) == 0) 11 { 12 dbf_ptr1 = (_UC ...
C 和 C++ 语言都是世界上最流行且使用最普遍的编程语言, 因此 Eclipse 平台(Eclipse Platform)提供对 C/C++ 开发的支持一点都不足为奇。 因为 Eclipse 平台只是用于开发者工具的一个框架,它不直接支持 C/C++;它...