java_I/O_合並流(SequenceInputStream)
阿新 • • 發佈:2018-07-28
str file pack main path pac row exist puts
- package testjava_13;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.SequenceInputStream;
- public class SequenceDemo {
- public static void main(String[] args) throws IOException {
- //聲明兩個文件讀入流
- File inputFile1 = createFile("E:"+File.separatorChar+"mytest"+File.separatorChar+"1.txt");
- File inputFile2 = createFile("E:"+File.separatorChar+"mytest"+File.separatorChar+"2.txt");
- File outputFile = createFile("E:"+File.separatorChar+"mytest"+File.separatorChar+"12.txt");
- InputStream inputStream1 = new FileInputStream(inputFile1),
- inputStream2=new FileInputStream(inputFile2);
- OutputStream outputStream = new FileOutputStream(outputFile);
- SequenceInputStream seInputStream = new SequenceInputStream(inputStream1,inputStream2);
- int c=-1;
- while ((c=seInputStream.read())!=-1) {
- outputStream.write(c);
- }
- inputStream1.close();
- inputStream2.close();
- outputStream.close();
- System.out.print("Ok~!");
- }
- //根據路徑創建File
- private static File createFile(String filePath) {
- File file = new File(filePath);
- if (!file.getParentFile().exists()) { //如果父級路徑不存在就創建父級目錄
- file.getParentFile().mkdirs();
- if (!file.exists()) { //如果文件不存在就創建文件
- try {
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return file;
- }
- }
java_I/O_合並流(SequenceInputStream)