Java知识点总结(JavaIO- 合并流类 )
@(Java知识点总结)[Java, JavaIO]
[toc]
合并流的主要功能是将两文件的内容合并成一个文件
public class Demo13 {
public static void main(String[] args) {
File f1 = new File("E:"+File.separator+"a.txt");
File f2 = new File("E:"+File.separator+"b.txt");
File f3 = new File("E:"+File.separator+"ab.txt");
InputStream is1,is2 =null;
OutputStream os =null;
try {
is1 = new FileInputStream(f1);
is2 = new FileInputStream(f2);
os = new FileOutputStream(f3);
SequenceInputStream sis = new SequenceInputStream(is1, is2);
System.out.println(sis.available()+"字节");
int temp = 0;
while((temp=sis.read())!=-1){
os.write(temp);
}
sis.close();
is1.close();
is2.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在多文件的应用场景下非常高效,因为是将多个流串联起来,形成一个流,便于操作