ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码

InputStream && OutputStream(1/3)

来源:网络整理     时间:2016-06-28     关键词:

本篇文章主要介绍了" InputStream && OutputStream",主要涉及到方面的内容,对于其他编程jrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下: InputStream && OutputStream介绍IO流操作中非常重要的一组接口(其实是抽象类)是InputStream和OutputStream。In...

InputStream && OutputStream


介绍

IO流操作中非常重要的一组接口(其实是抽象类)是InputStream和OutputStream。

InputStream字节输入流其最核心的一个方法是read()方法
OutputStream字节输出流其最核心的一个方法是write()方法

所有字节输入输都要实现read方法,所有字节输出流都要实现write()方法。
字节流可以操作任意类型的文件(二进制或文本文件)

首先了解几个概念:

01机器码:只有机器才能识别0101串
字节:拿英文来说明就是英文字符所对应的ASCII码(a==>97)
字符:就是我们人类能识别的字符文字
备注:每种语言有自己对应的字节码比如英文一个字符只占一个字节(ASCII编
码),但是中文可能就会占两个字节(gbk/gb2312)或三个字节(utf-8)取决于使用何种编码格式。

InputStream抽象类研究

InputStream核心Code

    /**
     * This abstract class is the superclass of all classes representing
     * an input stream of bytes.
     *
     * Applications that need to define a subclass of InputStream
     * must always provide a method that returns the next byte of input.
     */
    public abstract class InputStream implements Closeable {

        /**
         * Reads the next byte of data from the input stream. The value byte is
         * returned as an int in the range 0 to
         * 255. If no byte is available because the end of the stream
         * has been reached, the value -1 is returned. This method
         * blocks until input data is available, the end of the stream is detected,
         * or an exception is thrown.
         *
         * A subclass must provide an implementation of this method.
         */
        public abstract int read() throws IOException;//核心方法

        /**
         * Reads some number of bytes from the input stream and stores them into
         * the buffer array b. The number of bytes actually read is
         * returned as an integer.  This method blocks until input data is
         * available, end of file is detected, or an exception is thrown.
         *
         * If the length of b is zero, then no bytes are read and
         * 0 is returned; otherwise, there is an attempt to read at
         * least one byte. If no byte is available because the stream is at the
         * end of the file, the value -1 is returned; otherwise, at
         * least one byte is read and stored into b.
         *
         * The first byte read is stored into element b[0], the
         * next one into b[1], and so on. The number of bytes read is,
         * at most, equal to the length of b. Let k be the
         * number of bytes actually read; these bytes will be stored in elements
         * b[0] through b[k-1],
         * leaving elements b[k] through
         * b[b.length-1] unaffected.
         */
        public int read(byte b[]) throws IOException {
            return read(b, 0, b.length);
        }

        //理解下面代码块
        public int read(byte b[], int off, int len) throws IOException {
            if (b == null) {
                throw new NullPointerException();
            } else if (off < 0 || len < 0 || len > b.length - off) {
                throw new IndexOutOfBoundsException();
            } else if (len == 0) {
                return 0;
            }

            int c = read();
            if (c == -1) {
                return -1;
            }
            b[off] = (byte)c;

            int i = 1;
            try {
                for (; i < len ; i++) {
                    c = read();
                    if (c == -1) {
                        break;
                    }
                    b[off + i] = (byte)c;
                }
            } catch (IOException ee) {
            }
            return i;
        }

        /**
         * Closes this input stream and releases any system resources associated
         * with the stream.
         */
        public void close() throws IOException {}

    //备注:最重要的是理解read()方法和read(byte[] bytes)方法

}

典型模板代码

相关图片

相关文章