码迷,mamicode.com
首页 > 其他好文 > 详细

(20)IO流之SequenceInputStream 序列流

时间:2016-11-03 14:17:39      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:add   使用   code   分享   逻辑   技术分享   行合并   合并   dem   

序列流,对多个流进行合并。

SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。

 

序列流,对多个流进行合并。

 

SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。

 

合并两个流

 

使用构造函数SequenceInputStream(InputStream s1, InputStream s2)

 

 

 

技术分享
 1 //使用SequenceInputStream合并两个文件
 2     public static void merge2() throws IOException{
 3         //找到目标文件
 4         File inFile1 = new File("d:\\a.txt");
 5         File inFile2 = new File("d:\\b.txt");
 6         File outFile = new File("D:\\c.txt");
 7         //建立数据通道
 8         FileOutputStream fileOutputStream = new FileOutputStream(outFile);
 9         FileInputStream fileInputStream1 = new FileInputStream(inFile1);
10         FileInputStream fileInputStream2 = new FileInputStream(inFile2);
11         
12         //建立序列流对象
13         SequenceInputStream inputStream = new SequenceInputStream(fileInputStream1,fileInputStream2);
14         byte[] buf = new byte[1024];
15         int length = 0;
16         while((length = inputStream.read(buf))!=-1)
17         {
18             fileOutputStream.write(buf, 0, length);
19         }
20         inputStream.close();
21 //        fileInputStream1.close();    //查看上面inputStream.close()的源码就可以看到,它穿起来的流一块关闭了
22 //        fileInputStream2.close();
23         fileOutputStream.close();
24 }
View Code

 

合并多个文件

技术分享
 1 //把三个文件合并成一个文件
 2     public static void merge3() throws IOException
 3     {
 4         //找到目标文件
 5         File file1 = new File("d:\\a.txt");
 6         File file2 = new File("d:\\b.txt");
 7         File file3 = new File("D:\\c.txt");
 8         File file4 = new File("d:\\d.txt");
 9         //建立对应的输入输出流对象
10         FileInputStream fileInputStream1 = new FileInputStream(file1);
11         FileInputStream fileInputStream2 = new FileInputStream(file2);
12         FileInputStream fileInputStream3 = new FileInputStream(file3);
13         FileOutputStream fileOutputStream = new FileOutputStream(file4);
14         
15         //创建序列流对象
16         Vector<FileInputStream> vector = new Vector<FileInputStream>();
17         vector.add(fileInputStream1);
18         vector.add(fileInputStream2);
19         vector.add(fileInputStream3);
20         Enumeration<FileInputStream> e = vector.elements();
21         
22         SequenceInputStream inputStream = new SequenceInputStream(e);
23         
24         //读取文件的数据
25         int length =0;
26         byte[] buf = new byte[1024];
27         while((length = inputStream.read(buf))!=-1)
28         {
29             fileOutputStream.write(buf, 0, length);
30         }
31         inputStream.close();
32         fileOutputStream.close();
33     }
View Code

下面是一个例子把一个mp3文件切割合并的过程:

技术分享
 1 public class Demo2
 2 {
 3     public static void main(String[] args) throws IOException
 4     {
 5         cutFile();
 6         mergeFile();
 7     }
 8     
 9     //合并
10     public static void mergeFile() throws IOException
11     {
12         //找到目标文件
13         File dir = new File("D:\\part");
14         Vector<FileInputStream> vector = new Vector<FileInputStream>();
15         //通过目标文件夹找到所有的mmp3并添加到Vector中
16         File[] files =  dir.listFiles();
17         for (File file : files)
18         {
19             if(file.getName().endsWith("mp3"))
20             {
21                 vector.add(new FileInputStream(file));
22             }
23         }
24         
25         //通过vector获取迭代器对象
26         Enumeration<FileInputStream> e =  vector.elements();
27         
28         SequenceInputStream inputStream = new SequenceInputStream(e);
29         
30         //建立文件的输出通道
31         FileOutputStream fileOutputStream = new FileOutputStream("D:\\merge.mp3");
32         //建立缓冲数组
33         int length = 0;
34         byte[] buf = new byte[1024*1024];
35         while((length = inputStream.read(buf))!=-1)
36         {
37             fileOutputStream.write(buf, 0, length);
38         }
39     }
40     
41     
42     //切割
43     public static void cutFile() throws IOException
44     {
45         File file = new File("D:\\1.mp3");
46         //目标文件夹
47         File dir = new File("D:\\part");
48         //建立数据的输入通道
49         FileInputStream fileInputStream = new FileInputStream(file);
50         //建立缓存数组存储
51         byte[] buf = new byte[1024*1024];
52         int length = 0;
53         for(int i = 1 ; (length = fileInputStream.read(buf))!=-1; i ++)
54         {
55             FileOutputStream fileOutputStream = new FileOutputStream(new File(dir, "part"+i+".mp3"));
56             fileOutputStream.write(buf, 0, length);
57             fileOutputStream.close();
58         }
59         //关闭资源
60         fileInputStream.close();
61     }
62 }
View Code

 

(20)IO流之SequenceInputStream 序列流

标签:add   使用   code   分享   逻辑   技术分享   行合并   合并   dem   

原文地址:http://www.cnblogs.com/OliverZhang/p/6026155.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!