import java.io.*;public class TestData{
public static void main(String[] agrs){
try{
ByteArrayOutputStream out = new 
ByteArrayOutputStream();
DataOutputStream output = 
new DataOutputStream(out);
output.writeInt(98);
output.writeUTF("YANGYONGJIE");
ByteArrayInputStream in = new 
ByteArrayInputStream(out.toByteArray());
DataInputStream input = 
new DataInputStream(in);
System.out.println("读出了" + in.available() + "字节");
int a = input.readInt();
String tem = input.readUTF();

System.out.print(a + " " + tem);
}
catch(IOException e){
}
}
}
代码是对的 你们运行下看看 ! 我的问题是 它为什么显示的是17 个字节 ! "yangyongjie" + int 怎么算也是15个啊 就算char 占2个字节 那也是26啊; 怎么会是17呢 ! 原来以为字符串后面跟有\n 但是把"yangyongjie" 打印出来又没有 ! 谁能我分析下 ?

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【yangyongjie81】截止到2008-06-29 09:48:59的历史汇总数据(不包括此帖):
    发帖数:6                  发帖分:120                
    结贴数:6                  结贴分:120                
    未结数:0                  未结分:0                  
    结贴率:100.00%            结分率:100.00%            
    敬礼!
      

  2.   

    关键是这句  output.writeUTF("YANGYONGJIE"); // writeUTF的缘故!Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner. First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the stringlz了解了么,hoho~
      

  3.   

    中文API的解释:
    writeUTF
    public final void writeUTF(String str)
                        throws IOException以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流。 
    首先,通过 writeShort 方法将两个字节写入输出流,表示后跟的字节数。该值是实际写出的字节数,不是字符串的长度