Java SequenceInputStream read()方法

java.io.SequenceInputStream.read() 用于从输入流中读取下一个数据字节。

1 语法

public int read()

2 参数

3 返回值

返回数据的下一个字节,如果到达流的末尾,则返回-1。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.SequenceInputStream.read()方法的例子
 */
import java.io.*;

public class Demo {
    public static void main(String[] args) {

        // create two  new strings with 5 characters each
        String s1 = "Hello";
        String s2 = "World";

        // create 2 input streams
        byte[] b1 = s1.getBytes();
        byte[] b2 = s2.getBytes();
        ByteArrayInputStream is1 = new ByteArrayInputStream(b1);
        ByteArrayInputStream is2 = new ByteArrayInputStream(b2);

        // create a new Sequence Input Stream
        SequenceInputStream sis = new SequenceInputStream(is1, is2);

        try {
            // read 10 characters, 5 from each stream
            for (int i = 0; i < 10; i++) {
                char c = (char) sis.read();
                System.out.print("" + c);
            }

            // change line
            System.out.println();

            // close the streams
            sis.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

输出结果为:

HelloWorld

热门文章

优秀文章