数据流

DataInputStream

继承关系:

class DataInputStream extends FilterInputStream implements DataInput

FilterInputStream:
class FilterInputStream extends InputStream

构造方法:

InputStream in:流实例,所以是高级流
 public DataInputStream(InputStream in) {
        super(in);
    }
 protected FilterInputStream(InputStream in) {
        this.in = in;
    }

特殊方法:

public final static String readUTF(DataInput in) throws IOException {
        int utflen = in.readUnsignedShort();
        byte[] bytearr = null;
        char[] chararr = null;
        if (in instanceof DataInputStream) {
            DataInputStream dis = (DataInputStream)in;
            if (dis.bytearr.length < utflen){
                dis.bytearr = new byte[utflen*2];
                dis.chararr = new char[utflen*2];
            }
            chararr = dis.chararr;
            bytearr = dis.bytearr;
        }else {
            bytearr = new byte[utflen];
            chararr = new char[utflen];
        }
        in.readFully(bytearr, 0, utflen);
           while (count < utflen) {
            c = (int) bytearr[count] & 0xff;
            if (c > 127) break;
            count++;
            chararr[chararr_count++]=(char)c;
        }
        while (count < utflen) {
            c = (int) bytearr[count] & 0xff;
            switch (c >> 4) {
        case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7             
                 count++;
                    chararr[chararr_count++]=(char)c;
                    break;
                case 12: case 13:

                    count += 2;
                    if (count > utflen)
                        throw new UTFDataFormatException(
                            "malformed input: partial character at end");
                    char2 = (int) bytearr[count-1];
                    if ((char2 & 0xC0) != 0x80)
                        throw new UTFDataFormatException(
                            "malformed input around byte " + count);
                    chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
                                                    (char2 & 0x3F));
                    break;
                case 14:unt += 3;
                    if (count > utflen)
                        throw new UTFDataFormatException(
                            "malformed input: partial character at end");
                    char2 = (int) bytearr[count-2];
                    char3 = (int) bytearr[count-1];
           if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
              throw new UTFDataFormatException(
                            "malformed input around byte " + (count-1));
                  chararr[chararr_count++]=(char)(((c     & 0x0F) << 12) |
                   ((char2 & 0x3F) << 6)  | ((char3 & 0x3F) << 0));
                    break;
                default:
                throw new UTFDataFormatException(
                        "malformed input around byte " + count);
            }
        }
       return new String(chararr, 0, chararr_count);
  }

 

DataInputStream使用示例代码:

public static void main(String[] args) {
    String s="E:\\IO\\readerFile.txt";
    read(s);
    }
    public static void read(String path) {
        try {
            FileInputStream fileInputStream=new FileInputStream(path);
            DataInputStream dataInputStream=new DataInputStream(fileInputStream);
            System.out.println(dataInputStream.read());
            System.out.println(dataInputStream.readBoolean());
            System.out.println( dataInputStream.readUTF());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

DataOutputStream

继承关系:

class DataOutputStream extends FilterOutputStream implements DataOutput
FilterOutputStream:
class FilterOutputStream extends OutputStream

构造函数:

高级流:OutputStream out
 public DataOutputStream(OutputStream out) {
        super(out);
    }

特有方法:

static int writeUTF(String str, DataOutput out) throws IOException {
        int strlen = str.length();
        int utflen = 0;
        int c, count = 0;
    /* use charAt instead of copying String to char array */
        for (int i = 0; i < strlen; i++) {
            c = str.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                utflen++;
            } else if (c > 0x07FF) {
                utflen += 3;
            } else {
                utflen += 2;
            }
        }
        if (utflen > 65535)
            throw new UTFDataFormatException(
                "encoded string too long: " + utflen + " bytes");
        byte[] bytearr = null;
        if (out instanceof DataOutputStream) {
            DataOutputStream dos = (DataOutputStream)out;
            if(dos.bytearr == null || (dos.bytearr.length < (utflen+2)))
                dos.bytearr = new byte[(utflen*2) + 2];
            bytearr = dos.bytearr;
        } else {
            bytearr = new byte[utflen+2];
        }
        bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
        bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);
        int i=0;
        for (i=0; i<strlen; i++) {
           c = str.charAt(i);
           if (!((c >= 0x0001) && (c <= 0x007F))) break;
           bytearr[count++] = (byte) c;
        }
         for (;i < strlen; i++){
            c = str.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                bytearr[count++] = (byte) c;
            } else if (c > 0x07FF) {
                bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
                bytearr[count++] = (byte) (0x80 | ((c >>  6) & 0x3F));
                bytearr[count++] = (byte) (0x80 | ((c >>  0) & 0x3F));
            } else {
                bytearr[count++] = (byte) (0xC0 | ((c >>  6) & 0x1F));
                bytearr[count++] = (byte) (0x80 | ((c >>  0) & 0x3F));
            }
        }
        out.write(bytearr, 0, utflen+2);
        return utflen + 2;
    }
    /**
     * Returns the current value of the counter <code>written</code>,
     * the number of bytes written to this data output stream so far.
     * If the counter overflows, it will be wrapped to Integer.MAX_VALUE.
     *
     * @return  the value of the <code>written</code> field.
     * @see     java.io.DataOutputStream#written
     */
    public final int size() {
        return written;
    }

DataInputStream使用示例代码:

public static void main(String[] args) {
String s="E:\\IO\\readerFile.txt";
write(s);
}
public static void write(String path) {
    try {
        FileOutputStream fileOutputStream=new FileOutputStream(path);
        DataOutputStream dataOutputStream=new         
        DataOutputStream(fileOutputStream);
        dataOutputStream.write(121);
        dataOutputStream.writeBoolean(true);
        dataOutputStream.writeUTF("hell0");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

底层实现
eg: readInt()
int:4个字节 32个比特位
in.read():一次只读取一个字节
为保证读取正确:(ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)

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));
    }

适用场景
适用场景:excel 等多个数据类型的应用场所
优点:
与机器底层无关,根据java字节大小进行读写
缺点:
必须按顺序读取

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值