import java.io.* ;class IO{ public static void main(String[] args){
byte buffer[] = new byte[2056];
try{
while(true){
int bytes = System.in.read(buffer,0,2056);
String str = new String(buffer,0,bytes);
System.out.println("-->"+str+":"+bytes+":"+str.length());
}
}
catch( Exception e){
String err = e.toString();
System.out.println(err);
}
}
}
//执行情况
//12中国34
//-->12中国34
//:10:8     想知道10和8分别怎么出来的?为何是10和8,为什么有的占1有的占2个字节

解决方案 »

  1.   

    运行一下面的代码:public class Test{
        public static void main(String args[])  throws Exception{
         byte buffer[] = new byte[2056]; 
         try{
         int bytes = System.in.read(buffer,0,2056); 
         String str = new String(buffer,0,bytes); 
         System.out.println("-->"+str+":"+bytes+":"+str.length()); 
         //做以下测试:
         System.out.println("-----测试-----");
         char[] chars=str.toCharArray();
         for(char c :chars){
         System.out.println((int)c);
         }
         System.out.println("回车符的UNICODE是"+(int)'\r');
         System.out.println("换行符的UNICODE是"+(int)'\n');
         }catch( Exception e){ 
         String err = e.toString(); 
         System.out.println(err); 
         } 
        }
    }结果如下:F:\java>java Test
    12中国34
    -->12中国34
    :10:8
    -----测试-----
    49
    50
    20013
    22269
    51
    52
    13
    10
    回车符的UNICODE是13
    换行符的UNICODE是10