1. 程式人生 > >java_I/O_合並流(SequenceInputStream)

java_I/O_合並流(SequenceInputStream)

str file pack main path pac row exist puts

  1. package testjava_13;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.SequenceInputStream;
  9. public class SequenceDemo {
  10. public static void main(String[] args) throws IOException {
  11. //聲明兩個文件讀入流
  12. File inputFile1 = createFile("E:"+File.separatorChar+"mytest"+File.separatorChar+"1.txt");
  13. File inputFile2 = createFile("E:"+File.separatorChar+"mytest"+File.separatorChar+"2.txt");
  14. File outputFile = createFile("E:"+File.separatorChar+"mytest"+File.separatorChar+"12.txt");
  15. InputStream inputStream1 = new FileInputStream(inputFile1),
  16. inputStream2=new FileInputStream(inputFile2);
  17. OutputStream outputStream = new FileOutputStream(outputFile);
  18. SequenceInputStream seInputStream = new SequenceInputStream(inputStream1,inputStream2);
  19. int c=-1;
  20. while ((c=seInputStream.read())!=-1) {
  21. outputStream.write(c);
  22. }
  23. inputStream1.close();
  24. inputStream2.close();
  25. outputStream.close();
  26. System.out.print("Ok~!");
  27. }
  28. //根據路徑創建File
  29. private static File createFile(String filePath) {
  30. File file = new File(filePath);
  31. if (!file.getParentFile().exists()) { //如果父級路徑不存在就創建父級目錄
  32. file.getParentFile().mkdirs();
  33. if (!file.exists()) { //如果文件不存在就創建文件
  34. try {
  35. file.createNewFile();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41. return file;
  42. }
  43. }

java_I/O_合並流(SequenceInputStream)