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?
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?
另外,能不能说下java中输入输出流的机制,它是怎样实现的,好像不同于C++中的输入流机制。
缓冲区的机制又是怎样的?看到书上说“当向一个缓冲流写入数据时,系统将数据发送到缓冲区,而不是直接发送到外部设备。缓冲区自动记录数据,当缓冲区满时,系统将数据全部发送到相应的设备”,难道所谓的缓冲区就是一个数组?之前都以为是系统专门分配的一块专用区域
谢谢各位解答!
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str="";
try {
str=in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}