System.in.read(buffer)输入字符计数

beijihu3 2007-11-06 08:45:07
import java.io.* ;
public class Input1
{
public static void main(String args[]) throws IOException
{
System.out.println("Input:");
byte buffer[]=new byte[512]; //创建缓冲区
int count=System.in.read(buffer); //读取标准输入流
System.out.println("Output:");
for(int i=0;i<count;i++)
System.out.print(" "+buffer[i]) ; //按字节输出
System.out.println();
for(int i=0;i<count;i++)
System.out.print((char)buffer[i]); //按字符输出
System.out.println("count="+count); //buffer实际长度
}
}



程序运行时,从键盘输入4个字符“asdf'’并按Enter键。而保存在缓冲区buffer
中的实际元素个数count为6.

问: count为什么是6?
...全文
489 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
咖啡机 2010-12-22
  • 打赏
  • 举报
回复
jf…………………………………………………………………………………………………………………………
Kreocn 2007-11-08
  • 打赏
  • 举报
回复
LZ还是用这个输入字符吧,得到一个字符串,用.length方法就可以得到长度了...比较准确

public static String readString() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str="";
try {
str=in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
lord_is_layuping 2007-11-07
  • 打赏
  • 举报
回复
\r\n
lihaifeng0412 2007-11-07
  • 打赏
  • 举报
回复
回车并换行
beijihu3 2007-11-07
  • 打赏
  • 举报
回复
\r是因为按下了回车键吧?但为什么会有\n呢?
另外,能不能说下java中输入输出流的机制,它是怎样实现的,好像不同于C++中的输入流机制。
缓冲区的机制又是怎样的?看到书上说“当向一个缓冲流写入数据时,系统将数据发送到缓冲区,而不是直接发送到外部设备。缓冲区自动记录数据,当缓冲区满时,系统将数据全部发送到相应的设备”,难道所谓的缓冲区就是一个数组?之前都以为是系统专门分配的一块专用区域。。。
谢谢各位解答!
网络咖啡 2007-11-06
  • 打赏
  • 举报
回复
Windows上敲回车时,相当于输入\r\n

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧