谁帮我看下这个问题!

yangyongjie81 2008-06-29 09:48:58
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" 打印出来又没有 ! 谁能我分析下 ?
...全文
54 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhj92lxs 2008-06-29
  • 打赏
  • 举报
回复
ls的英语真好!
bootupnow 2008-06-29
  • 打赏
  • 举报
回复
中文API的解释:
writeUTF
public final void writeUTF(String str)
throws IOException以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流。
首先,通过 writeShort 方法将两个字节写入输出流,表示后跟的字节数。该值是实际写出的字节数,不是字符串的长度
bootupnow 2008-06-29
  • 打赏
  • 举报
回复
关键是这句 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 string

lz了解了么,hoho~

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧