A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© endlessjoker 中级黑马   /  2014-3-25 13:18  /  1427 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

DataInputStream和DataOutputStream不是更好用一些吗,为什么很少见呢?

1 个回复

倒序浏览
DataInputStream和DataOutputStream属于过滤流,使用节点流作为输入或输出。
过滤流是使用一个已经存在的输入流或输出流连接创建的。
它提供了读写Java中的基本数据类型的功能。
DatainputStream部分源码:
// 从“数据输入流”中读取boolean类型的值  
    public final boolean readBoolean() throws IOException {  
        int ch = in.read();  
        if (ch < 0)  
            throw new EOFException();  
        return (ch != 0);  
    }  

    // 从“数据输入流”中读取Byte类型的值  
    public final byte readByte() throws IOException {  
        int ch = in.read();  
        if (ch < 0)  
            throw new EOFException();  
        return (byte)(ch);  
    }  

    // 从“数据输入流”中读取“无符号的Byte类型”的值,即读取值为正数的byte值  
    public final int readUnsignedByte() throws IOException {  
        int ch = in.read();  
        if (ch < 0)  
            throw new EOFException();  
        return ch;  
    }  

    // 从“数据输入流”中读取“short类型”的值  
    public final short readShort() throws IOException {  
        int ch1 = in.read();  
        int ch2 = in.read();  
        if ((ch1 | ch2) < 0)  
            throw new EOFException();  
        return (short)((ch1 << 8) + (ch2 << 0));  
    }  

    // 从“数据输入流”中读取“无符号的short类型”的值  
    public final int readUnsignedShort() throws IOException {  
        int ch1 = in.read();  
        int ch2 = in.read();  
        if ((ch1 | ch2) < 0)  
            throw new EOFException();  
        return (ch1 << 8) + (ch2 << 0);  
    }  

    // 从“数据输入流”中读取“char类型”的值  
    public final char readChar() throws IOException {  
        int ch1 = in.read();  
        int ch2 = in.read();  
        if ((ch1 | ch2) < 0)  
            throw new EOFException();  
        return (char)((ch1 << 8) + (ch2 << 0));  
    }  

    // 从“数据输入流”中读取“int类型”的值  
    public final int readInt() 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 readBuffer[] = new byte[8];  

    // 从“数据输入流”中读取“long类型”的值  
    public final long readLong() throws IOException {  
        readFully(readBuffer, 0, 8);  
        return (((long)readBuffer[0] << 56) +  
                ((long)(readBuffer[1] & 255) << 48) +  
                ((long)(readBuffer[2] & 255) << 40) +  
                ((long)(readBuffer[3] & 255) << 32) +  
                ((long)(readBuffer[4] & 255) << 24) +  
                ((readBuffer[5] & 255) << 16) +  
                ((readBuffer[6] & 255) <<  8) +  
                ((readBuffer[7] & 255) <<  0));  
    }  

    // 从“数据输入流”中读取“float类型”的值  
    public final float readFloat() throws IOException {  
        return Float.intBitsToFloat(readInt());  
    }  

    // 从“数据输入流”中读取“double类型”的值  
    public final double readDouble() throws IOException {  
        return Double.longBitsToDouble(readLong());  
    }  
------------------------------------------------------------------------------------------------------
如果你根本不关心你读取结果的类型,那你使用过滤流不就是做了许多无用的操作,你懂的:)
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马