合并流加包装流

java1.8之后 把创建流写入try()的括号中,就不用自己去关闭流了

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.SequenceInputStream;

public class SequenceInputStreamDemo2 {

public static void main(String[] args) {
// 创建文件对象
File file1 = new File("C:\\Users\\Administrator\\Desktop\\b.txt");
File file2 = new File("C:\\Users\\Administrator\\Desktop\\a.txt");
                // 字节文件输入流
try (        FileInputStream fileInputStream1 = new FileInputStream(file1);
FileInputStream fileInputStream2 = new FileInputStream(file2);
// 包装流
BufferedInputStream bis1 = new BufferedInputStream(fileInputStream1);
BufferedInputStream bis2 = new BufferedInputStream(fileInputStream2);
// 合并流

SequenceInputStream sis = new SequenceInputStream(bis1, bis2);

                                //文件字节输出流

FileOutputStream out = new FileOutputStream(new File("D:\\c.txt"))) {

int len = 0;
byte[] bt = new byte[1024];
while ((len = sis.read(bt)) != -1) {
out.write(bt, 0, len);
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}

}


阅读更多

没有更多推荐了,返回首页