zoukankan      html  css  js  c++  java
  • ByteArrayXXXputStream、InputStream的read()方法

    字节数组流:
    ByteArrayOutputStream:    可以捕获内存缓冲区的数据,转换成字节数组。
    ByteArrayoutputStream bout=new ByteArrayOutputStream();
    bout.write(int a);  bout.write(int b);  bout.write(int c);
    byte[] buf=bout.toByteArray();//获取内存缓冲中的数据
    for(int i=0;i<=buf.length;i++)
    {
      System.out.println(buf);
    }
    bout.close();
    注:通过调用reset()方法可以重新定位。
    ByteArrayInputStream: 可以将字节数组转化为输入流
    ByteArrayInputStream bin=new ByteArrayInputStream(byte[] buf);
    int data=0;
    while( (b=bin.read())!=-1)
    {
      System.out.println(b);
    }
    bin.close();

    与DataOutputStream&DataInputStream联合使用:

    ByteArrayOutputStream bout=new ByteArrayOutputStream();
    DataOutputStream dos=new DataOutputStream(bout);
    String name="suntao";
    int age=19;
    dos.writeUTF(name);
    dos.writeInt(age);
    byte[] buf=bout.toByteArray();//获取内存缓冲区中的数据
    dos.close();
    bout.close();

    ByteArrayInputStream bin=new ByteArrayInputStream(byte[] buf);
    DataInputStream dis=new DataInputStream(bin);
    String name=dis.readUTF();//从字节数组中读取
    int age=dis.readInt();
    dis.close();
    bin.close();

    注:  DataInputStream&DataOutputStream还可以与FileInputStream&FileOutputStream
    联合使用。
    其中:
    DataInputStream&DataOutputStream关心如何将数据从高层次的形式转化成低层次的形式.
    FileInputStream&FileOutputStream关心如何操作存储单元以接受和产生数据。

    InputStream与DataInputStream区别

    DataInputStream类继承了InputStream,同是实现了DataInput接口,也就是说比普通的InputStream多一些方法。
    增加方法如下:

    public abstract void readFully(byte abyte0[])
            throws IOException;

        public abstract void readFully(byte abyte0[], int i, int j)
            throws IOException;

        public abstract int skipBytes(int i)
            throws IOException;

        public abstract boolean readBoolean()
            throws IOException;

        public abstract byte readByte()
            throws IOException;

        public abstract int readUnsignedByte()
            throws IOException;

        public abstract short readShort()
            throws IOException;

        public abstract int readUnsignedShort()
            throws IOException;

        public abstract char readChar()
            throws IOException;

        public abstract int readInt()
            throws IOException;

        public abstract long readLong()
            throws IOException;

        public abstract float readFloat()
            throws IOException;

        public abstract double readDouble()
            throws IOException;

        public abstract String readUTF()
            throws IOException

    InputStream的read方法API

    read
    public int read(byte[] b) throws
    从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数。在输入数据可用、检测到文件末尾或者抛出异常前,此方法一直阻塞。

    如果 b 的长度为 0,则不读取任何字节并返回 0;否则,尝试读取至少一个字节。如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少读取一个字节并将其存储在 b 中。

    将读取的第一个字节存储在元素 b[0] 中,下一个存储在 b[1] 中,依次类推。读取的字节数最多等于 b 的长度。设k为实际读取的字节数;这些字节将存储在 b[0] 到 b[k-1] 的元素中,不影响 b[k] 到 b[b.length-1] 的元素。

    类 InputStream 的 read(b) 方法的效果等同于:

    read(b, 0, b.length)
    参数:
    b - 存储读入数据的缓冲区。
    返回:
    读入缓冲区的总字节数;如果因为已经到达流末尾而不再有数据可用,则返回 -1。
    抛出:
    - 如果不是因为流位于文件末尾而无法读取第一个字节;如果输入流已关闭;如果发生其他 I/O 错误。
    - 如果 b 为 null。
    另请参见:
    public int read(byte[] b, int off, int len) throws
    将输入流中最多 len 个数据字节读入 byte 数组。尝试读取 len 个字节,但读取的字节也可能小于该值。以整数形式返回实际读取的字节数。

    在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。

    如果 len 为 0,则不读取任何字节并返回 0;否则,尝试读取至少一个字节。如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少读取一个字节并将其存储在 b 中。

    将读取的第一个字节存储在元素 b[off] 中,下一个存储在 b[off+1] 中,依次类推。读取的字节数最多等于 len。设k为实际读取的字节数;这些字节将存储在 b[off] 到 b[off+k-1] 的元素中,不影响 b[off+k] 到 b[off+len-1] 的元素。

    在任何情况下,b[0] 到 b[off] 的元素以及 b[off+len] 到 b[b.length-1] 的元素都不会受到影响。

    类 InputStream 的 read(b,off,len) 方法重复调用方法 read()。如果第一次这样的调用导致 IOException,则从对 read(b,off,len) 方法的调用中返回该异常。如果对 read() 的任何后续调用导致 IOException,则捕获该异常并将其视为到达文件末尾;到达该点时读取的字节存储在 b 中,并返回发生异常之前读取的字节数。在已读取输入数据 len 的请求数量、检测到文件结束标记、抛出异常前,此方法的默认实现将一直阻塞。建议子类提供此方法更为有效的实现。

    参数:
    b - 读入数据的缓冲区。
    off - 数组 b 中将写入数据的初始偏移量。
    len - 要读取的最大字节数。
    返回:
    读入缓冲区的总字节数;如果因为已到达流末尾而不再有数据可用,则返回 -1。
    抛出:
    - 如果不是因为位于文件末尾而无法读取第一个字节;如果输入流已关闭;如果发生其他 I/O 错误。
    - 如果 b 为 null。
    - 如果 off 为负,len 为负,或者 len 大于 b.length - off
  • 相关阅读:
    辛星和你彻底搞清CSS中的相对定位和绝对定位
    快速向表中插入大量数据Oracle中append与Nologging
    关于insert /*+ append*/ 各种insert插入速度比较
    dblink连接的目标端 session不断的问题。
    oracle操作记录
    ORACLE快速彻底Kill掉的会话
    Oracle 死锁的检测查询及处理
    Oracle 11g必须开启的服务及服务详细介绍
    oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务。
    Oracle报 ORA-00054资源正忙的解决办法
  • 原文地址:https://www.cnblogs.com/qingblog/p/2601146.html
Copyright © 2011-2022 走看看