iteye_7135 2010-06-06 11:29
浏览 231
已采纳

java io流的问题

[code="java"]public class ByteTester {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    out.write(259);//00000000 00000000 00000001 00000011

    byte [] buff = out.toByteArray();
    out.close();
    System.out.println("buff.length:="+buff.length);

    ByteArrayInputStream in = new ByteArrayInputStream(buff);
    int data;
    while((data=in.read())!=-1){
        System.out.println(data);
    }
    in.close();



}[/code]

输出结果:buff.length:=1
3

我初步分析是:out.write 只是写入最低位的一个字节,所以读取出才是3

如果是这样的话,写入了数字259 读取的却是3

java io 通过什么保证写入和读出的是一致的呢

  • 写回答

2条回答 默认 最新

  • hatpdb 2010-06-06 12:10
    关注

    数据类型错误,溢出了,你把259改成255以下试下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?