多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > InputStream && OutputStream

InputStream && OutputStream

來源:程序員人生   發布時間:2016-09-22 10:35:04 閱讀次數:2493次

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個數組中,當數組滿了以后然后將這個數組中所有字節輸入到輸出流中,然后清空數組繼續讀取字節。以此循環

}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 哪里可以看免费毛片 | 最新69国产成人精品视频69 | 最近中文字幕mv免费高清视频免费 | 中文字幕在线视频免费观看 | 日韩欧美一区二区三区在线观看 | 男女激情网站 | 中文字幕一级 | 在线伊人网 | asianjapanese日本护士 | 直接在线观看的三级网址 | 欧美一区二区三区在线观看免费 | 亚洲日韩精品欧美一区二区 | 日本国产亚洲 | 免费看一级欧美毛片 | 在线观看欧洲成人免费视频 | 久久精品一区二区三区不卡 | 美女无遮挡免费视频观看网站 | 亚洲欧美另类在线观看 | 午夜欧美在线 | 国产亚洲精品自在线观看 | 日本护士xxxo | 国产片欧美片亚洲片久久综合 | 久久久久免费 | 欧美一区二区久久精品 | 成人午夜免费在线观看 | 欧美色频| 午夜久久久久久 | 亚洲精品一二三四区 | 亚洲永久免费视频 | 色综合亚洲精品激情狠狠 | 日本天堂在线视频 | 男女啪啪片 | 成人免费毛片一区二区三区 | 一级欧美毛片成人 | 手机看片日韩在线 | 久久国产精品一区二区三区 | 中文在线视频观看 | 成人午夜精品久久久久久久小说 | 国产免费高清视频在线观看不卡 | 欧美特黄a级高清免费看片 欧美特黄一级aa毛片 | 久久久久日韩精品无 |