从键盘输入字符。
本例用System.in.read(buffer)从键盘输入一行字符,存储在缓冲区buffer中,count保存实际读入的字节个数,再以整数和字符两种方式输出buffer中的值。Read方法在java.io包中,而且要抛出IOException异常。程序如下:
import java.io.*;
public class ArrayTest {
public static void main(String[] args)throws Exception{
System.out.println("input please");
byte buff[]=new byte[1024];// 输入缓冲区
int count=System.in.read(buff);// 读取标准输入流
for(int i=0;i< count;i++){//输出buffer元素值
System.out.println((char)buff[i]);//按字符方式输出buffer
System.out.println("one");
System.out.println(buff[i]);
}
System.out.println("count="+count);// buffer实际长度
}
}
输出结果:
input please
a
a
one
97
one
13
one
10
count=3