那位高手路过,请指点一二!!!
我们都知道一个英语字母占一个字节,课时从下面的代码中发现,当我输入的是“lara”时,代码显示的字节数有6个,这是为什么呢??
下面是运行结果:
Input:lara
Output: 108 97 114 97 13 10
count = 6
lara
下面是代码:
import java.io.*;
public class KeyboardInput
{
public static void main(String args[]) throws IOException //抛出异常交由Java虚拟机处理
{
System.out.print("Input:");
byte buffer[] = new byte[512]; //以字节数组作为缓冲区
int count = System.in.read(buffer); //从标准输入流中读取若干字节到指定缓冲区,返回实际读取的字节数
System.out.print("Output: ");
for (int i=0; i<count; i++) //按字节方式输出buffer元素值
System.out.print(" "+buffer[i]);
System.out.println("\ncount = "+ count); //实际读取的字节数
for (int i=0; i<count; i++) //按字符方式输出buffer元素值
System.out.print((char) buffer[i]);
}
}