每天多一点之字节流、过滤流

1、流的用途

文件、网络、内存、标准输入输出的数据读写都需要用到流。

2、输入流、输出流

输入流:数据提供者,可从中读取数据出来

输出流:数据接收者,可往其中写数据

3、read()

在InputStream类中,方法read()提供了三种从流中读数据的方法:

n int read():从输入流中读一个字节,形成一个0~255之间的整数返回(是一个抽象方法)。

n int read(byte b[]):读多个字节到数组中,填满整个数组。

n int read(byte b[], int off, int len):从输入流中读取长度为len的数据,写入数组b中从索引off开始的位置,并返回读取得字节数。

对于这三个方法,若返回-1,表明流结束,否则,返回实际读取的字节数。

4、属于OutputStream类的方法有:

n write(int b):将一个整数输出到流中(只输出低位字节,为抽象方法)

n write(byte b[]):将字节数组中的数据输出到流中

n write(byte b[], int off, int len):将数组b中从off指定的位置开始,长度为len的数据输出到流中

n flush():刷空输出流,并将缓冲区中的数据强制送出

n close():关闭流

5、FileOutputStream、FileInputStream

/**
     * 将文件Test.txt复制到Text2.txt
     */    
    private FileOutputStream fos;
    private FileInputStream fis;

    public void testStreams() {
        try {
            fis = new FileInputStream("Test.txt");
            fos = new FileOutputStream("Test2.txt");

            byte[] bytearr = new byte[1024];
            int n = 0;
            while ((n = fis.read(bytearr)) != -1) {
                fos.write(bytearr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

6、FileOutputStream、FileInputStream、BufferedOutputStream、BufferedInputStream

/**
     * 将文件Test.txt复制到Text2.txt
     */
    private BufferedOutputStream bos;
    private BufferedInputStream bis;
    private FileOutputStream fos;
    private FileInputStream fis;

    public void testStreams() {
        try {
            fis = new FileInputStream("Test.txt");
            fos = new FileOutputStream("Test2.txt");
            bis = new BufferedInputStream(fis,1024);//默认为32字节,改为1024
            bos = new BufferedOutputStream(fos,1024);//默认为32字节,改为1024 
            int n = 0;
            while ((n = bis.read()) != -1) {
                bos.write(n);
            }

          out.flush(); //最后一次读取的数据可能不到1024字节   
        }
catch (Exception e) {
            e.printStackTrace();
        }
finally {
           
try {
                bos.close();
                bis.close();
                fos.close();
                fis.close();
            }
catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
           
        }
    }

7、合并流 SequenceInputStream

SequenceInputStream主要用来将2个流合并在一起,比如将两个txt中的内容合并为另外一个txt:

public void sequence(){
        File file1 = new File("d:" + File.separator + "hello1.txt");
        File file2 = new File("d:" + File.separator + "hello2.txt");
        File file3 = new File("d:" + File.separator + "hello.txt");
        InputStream input1 = new FileInputStream(file1);
        InputStream input2 = new FileInputStream(file2);
        OutputStream output = new FileOutputStream(file3);
        // 合并流
        SequenceInputStream sis = new SequenceInputStream(input1, input2);
        int temp = 0;
        while((temp = sis.read()) != -1){
            output.write(temp);
        }
        input1.close();
        input2.close();
        output.close();
        sis.close();
}
原文地址:https://www.cnblogs.com/hackerd/p/3091863.html