首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java源读写数据

2012-12-26 
Java流读写数据一个比较复杂的情况是流中的数据类型各不相同,可能是A+B+C,对A,B,C的读写处理是A,B,C的类型

Java流读写数据
一个比较复杂的情况是流中的数据类型各不相同,可能是A+B+C,对A,B,C的读写处理是A,B,C的类型来决定。

1>在往流中写入数据之前要写入数据的类型,
2>从流中读数据的时候也要先判断数据类型

    private int readInt(InputStream in) throws IOException    {        int ch1 = in.read();        int ch2 = in.read();        int ch3 = in.read();        int ch4 = in.read();        if ((ch1 | ch2 | ch3 | ch4) < 0)            throw new EOFException();                return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));    }        private byte readByte(InputStream in) throws IOException    {        int b = in.read();        if(b < 0)            throw new EOFException();        return (byte)b;    }        private String readString(InputStream in) throws IOException    {        int len = readInt(in);        byte b[] = new byte[len];        int l = 0;        while(l < len)            l += in.read(b, l, len - l);        return new String(b);    }        private void writeInt(OutputStream out, int v) throws IOException    {        out.write((v >>> 24) & 0xFF);        out.write((v >>> 16) & 0xFF);        out.write((v >>>  8) & 0xFF);        out.write((v >>>  0) & 0xFF);    }        private void writeString(OutputStream out, String v) throws IOException    {        byte[] b = v.getBytes();        writeInt(out, b.length);        out.write(b);    }  

热点排行