国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > InputStream && OutputStream

InputStream && OutputStream

来源:程序员人生   发布时间:2016-09-22 10:35:04 阅读次数:2337次

InputStream && OutputStream


介绍

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

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

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

首先了解几个概念:

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

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 ⑴ 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 ⑴ 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⑴],
         * leaving elements b[k] through
         * b[b.length⑴] 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 == ⑴) {
                return ⑴;
            }
            b[off] = (byte)c;

            int i = 1;
            try {
                for (; i < len ; i++) {
                    c = read();
                    if (c == ⑴) {
                        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)方法

}

典型模板代码

@Test
public void testReadFile() throws Exception {

    String SEPARATOR = File.separator;
    File file = new File("E:" + SEPARATOR + "io" + SEPARATOR + "test.txt");
    // 取得输入流
    FileInputStream inputStream = new FileInputStream(file);

    // 定义缓存字节数组
    byte[] buffer = new byte[1024];
    int length = 0;// 表示实际读取字节数组缓冲的字节数目
    while ((length = inputStream.read(buffer)) != ⑴) {
        System.out.print(new String(buffer, 0, length, "utf⑻"));
        // 输出文件到控制台时最好不要使用ln换行打印,否则如果数组长度定义不恰当是可能出现问题的.
    }
    // 流资源关闭
    inputStream.close();

}

OutputStream抽象类研究

OutputStream核心Code

/**
 * This abstract class is the superclass of all classes representing
 * an output stream of bytes. An output stream accepts output bytes
 * and sends them to some sink.
 * 
 * Applications that need to define a subclass of
 * OutputStream must always provide at least a method
 * that writes one byte of output.
 */
public abstract class OutputStream implements Closeable, Flushable {
        /**
         * Writes the specified byte to this output stream. The general
         * contract for write is that one byte is written
         * to the output stream. The byte to be written is the eight
         * low-order bits of the argument b. The 24
         * high-order bits of b are ignored.
         * 
         * Subclasses of OutputStream must provide an
         * implementation for this method.
         *
         */
        public abstract void write(int b) throws IOException;

        /**
         * Writes b.length bytes from the specified byte array
         * to this output stream. The general contract for write(b)
         * is that it should have exactly the same effect as the call
         * write(b, 0, b.length).
         */
        public void write(byte b[]) throws IOException {
            write(b, 0, b.length);
        }

        /**
         * Writes len bytes from the specified byte array
         * starting at offset off to this output stream.
         * The general contract for write(b, off, len) is that
         * some of the bytes in the array b are written to the
         * output stream in order; element b[off] is the first
         * byte written and b[off+len⑴] is the last byte written
         * by this operation.
         * <p>
         * The write method of OutputStream calls
         * the write method of one argument on each of the bytes to be
         * written out. Subclasses are encouraged to override this method and
         * provide a more efficient implementation.
         */
        public void write(byte b[], int off, int len) throws IOException {
            if (b == null) {
                throw new NullPointerException();
            } else if ((off < 0) || (off > b.length) || (len < 0) ||
                       ((off + len) > b.length) || ((off + len) < 0)) {
                throw new IndexOutOfBoundsException();
            } else if (len == 0) {
                return;
            }
            for (int i = 0 ; i < len ; i++) {
                write(b[off + i]);
            }
        }

        /**
         * Flushes this output stream and forces any buffered output bytes
         * to be written out. The general contract of flush is
         * that calling it is an indication that, if any bytes previously
         * written have been buffered by the implementation of the output
         * stream, such bytes should immediately be written to their
         * intended destination.
         * 
         * If the intended destination of this stream is an abstraction provided by
         * the underlying operating system, for example a file, then flushing the
         * stream guarantees only that bytes previously written to the stream are
         * passed to the operating system for writing; it does not guarantee that
         * they are actually written to a physical device such as a disk drive.
         *
         * The flush method of OutputStream does nothing..
         */
        public void flush() throws IOException {
        }

        /**
         * Closes this output stream and releases any system resources
         * associated with this stream. The general contract of close
         * is that it closes the output stream. A closed stream cannot perform
         * output operations and cannot be reopened.
         */
        public void close() throws IOException {
        }

}
//有1位计算机科学家说缓存思想是20世纪计算机发展最重要的思想

典型模板代码

@Test
public void testFileOutputStream() throws Exception {

        String SEPARATOR = File.separator;
        File file = new File("E:" + SEPARATOR + "io" + SEPARATOR + "out.txt");
        // 定义输出流
        FileOutputStream out = new FileOutputStream(file);
        // 向输出流中写入数据
        out.write("helloworld 你好世界".getBytes("utf⑻"));
        out.close();

        // 备注使用OutputStream由于没有缓存,所以不需要调用flush方法就直接写入到文件中。
}

文件考本

说明:文件考本这里触及到源和目标其实这样就能够推行到网络上文件传输
也是1样的,都是有输入流和输出流,IO流操做弄清楚源和目标和输入输
出流和方向时那末问题就简单了模型就清楚了。

Code

@Test
public void testCopyFile() throws Exception {

    String SEPARATOR = File.separator;
    // 源文件
    File source = new File("E:" + SEPARATOR + "io" + SEPARATOR + "1.png");
    // 取得输入流
    FileInputStream inputStream = new FileInputStream(source);

    // 输出(目标)文件
    File copyFile = new File("E:" + SEPARATOR + "io" + SEPARATOR + "copy.png");
    // 定义输出流
    FileOutputStream outputStream = new FileOutputStream(copyFile);

    // 定义缓冲字节数组
    byte[] buffer = new byte[1024];
    int length = 0;
    while ((length = inputStream.read(buffer)) != ⑴) {// 只要输入流还有字节那末输出流就1直写入数据
        outputStream.write(buffer, 0, length);
    }
    outputStream.close();
    inputStream.close();

    // 备注:解释缓冲字节数组,通常缓冲字节数组都是对输入流而言的,可以那末理解,
    // 当输入流读1个文件的时候,本来是读1个字节就往输出流写1个字节,这样相对照较浪费时间,
    // 所以就能够读1定量的字节寄存到1个数组中,当数组满了以后然后将这个数组中所有字节输入到输出流中,然后清空数组继续读取字节。以此循环

}
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生