A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© Kevin.Kang 高级黑马   /  2015-7-29 17:38  /  435 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

合并流读写两个文件:
  1. package com.kxg_01;

  2. import java.io.BufferedOutputStream;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.SequenceInputStream;

  8. /*
  9. * 合并流:
  10. *                 SequenceInputStream
  11. *
  12. * 合并流读写两个文件
  13. *                 将a.txt和b.txt的内容写入到c.txt
  14. */
  15. public class SequenceDemo {
  16.         public static void main(String[] args) throws IOException {
  17.                 // 封装两个数据源
  18.                 InputStream is = new FileInputStream("a.txt");
  19.                 InputStream os = new FileInputStream("b.txt");

  20.                 // 创建合并流对象
  21.                 SequenceInputStream sis = new SequenceInputStream(is, os);

  22.                 // 封装目的地
  23.                 BufferedOutputStream bos = new BufferedOutputStream(
  24.                                 new FileOutputStream("c.txt"));
  25.                 int i = 0;
  26.                 while ((i = sis.read()) != -1) {
  27.                         bos.write(i);
  28.                 }
  29.                 bos.close();
  30.                 sis.close();
  31.         }
  32. }
复制代码



3 个回复

倒序浏览
合并流读写多个文件:
  1. package com.kxg_01;

  2. import java.io.BufferedOutputStream;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.SequenceInputStream;
  8. import java.util.Enumeration;
  9. import java.util.Vector;

  10. /*
  11. * 合并流读写多个文件
  12. *                 将a.txt,b.txt,c.txt的内容写入到copy.txt
  13. */
  14. public class SequenceDemo2 {
  15.         public static void main(String[] args) throws IOException {
  16.                 // SequenceInputStream(Enumeration<? extends InputStream>e)
  17.                 // 通过此构造方法可以读写多个文件,可以看出需要Enumeration类型的参数
  18.                 // Enumeration是Vector中elements()方法的返回值类型
  19.                
  20.                 // 创建Vector集合
  21.                 Vector<InputStream> v = new Vector<InputStream>();
  22.                
  23.                 // 封装数据源
  24.                 InputStream is1 = new FileInputStream("a.txt");
  25.                 InputStream is2 = new FileInputStream("b.txt");
  26.                 InputStream is3 = new FileInputStream("c.txt");
  27.                
  28.                 // 添加到集合中
  29.                 v.add(is1);
  30.                 v.add(is2);
  31.                 v.add(is3);
  32.                
  33.                 // 由集合方法elements()得到Enumeration类型数据
  34.                 Enumeration<InputStream> e = v.elements();
  35.                
  36.                 // 创建合并流对象
  37.                 SequenceInputStream sis = new SequenceInputStream(e);
  38.                
  39.                 // 封装目的地
  40.                 BufferedOutputStream bos = new BufferedOutputStream(
  41.                                 new FileOutputStream("copy.txt"));
  42.                 int i = 0;
  43.                 while ((i = sis.read()) != -1) {
  44.                         bos.write(i);
  45.                 }
  46.                 sis.close();
  47.                 bos.close();
  48.         }
  49. }
复制代码



回复 使用道具 举报
学习学习
回复 使用道具 举报
你钱真多。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马