java基础48 IO流技术(序列流)

本文知识点目录:

    1、SequenceInputStream序列流的步骤
    2、实例
    3、附录(音乐的切割与合并)



1、SequenceInputStream序列流的步骤

    1.找到目标文件
    2.建立数据输入输出通道
    3.建立序列流对象
    4.关闭资源

2、实例                       

  1 package com.dhb.file;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.io.SequenceInputStream;
  8 import java.util.ArrayList;
  9 import java.util.Enumeration;
 10 import java.util.Vector;
 11 
 12 /**
 13  * @author DSHORE / 2018-7-13
 14  *
 15  */
 16 /*
 17  * SequenceInputStream():序列流
 18  * */
 19 public class Demo21 {
 20 
 21     public static void main(String[] args) throws IOException {
 22         meger1();//字节流
 23         meger2();//序列流
 24         meger3();//序列流
 25     }
 26     
 27     //方式3:(序列流)把三个文件合成一个文件
 28     public static void meger3() throws IOException{
 29         //找到目标文件
 30         File file1 = new File("F:\a.txt");
 31         File file2 = new File("F:\b.txt");
 32         File file3 = new File("F:\c.txt");
 33         File file4 = new File("F:\d.txt");
 34         //建立数据输入输出通道
 35         FileOutputStream fos = new FileOutputStream(file4);
 36         FileInputStream fis1 = new FileInputStream(file1);
 37         FileInputStream fis2 = new FileInputStream(file2);
 38         FileInputStream fis3 = new FileInputStream(file3);
 39         //建立序列流
 40         Vector<FileInputStream> vector = new Vector<FileInputStream>();
 41         vector.add(fis1);
 42         vector.add(fis2);
 43         vector.add(fis3);
 44         Enumeration<FileInputStream> e = vector.elements();
 45         SequenceInputStream sequenceInputStream = new SequenceInputStream(e);
 46         //读取文件
 47         byte[] buf = new byte[1024];
 48         int length = 0;
 49         while((length = sequenceInputStream.read(buf)) != -1){
 50             fos.write(buf, 0, length);
 51         }
 52         //关闭资源
 53         sequenceInputStream.close();
 54         fos.close();
 55     }
 56     
 57     //方式2:(序列流) 需求:使用SequenceInputStream序列流合并a.txt与b.txt文件的内容
 58     public static void meger2() throws IOException{
 59         //找到目标文件
 60         File file1 = new File("F:\a.txt");
 61         File file2 = new File("F:\b.txt");
 62         File file3 = new File("F:\c.txt");
 63         //建立数据输入输出通道
 64         FileOutputStream fos = new FileOutputStream(file3);
 65         FileInputStream fis = new FileInputStream(file1);
 66         FileInputStream fis1 = new FileInputStream(file2);
 67         //建立我们的序列流对象
 68         SequenceInputStream sis = new SequenceInputStream(fis, fis1);
 69         byte[] buf = new byte[1024];
 70         int length = 0;
 71         while((length = sis.read(buf)) != -1){
 72             fos.write(buf, 0, length);
 73         }
 74         //关闭资源
 75         sis.close();
 76         fos.close();
 77     }
 78 
 79     //方式1:(字节流) 需求:把a.txt与b.txt文件内容合并.
 80     public static void meger1() throws IOException{
 81         //找到目标文件
 82         File file1=new File("F:\a.txt");
 83         File file2=new File("F:\b.txt");
 84         File file3=new File("F:\c.txt");
 85         //建立数据输入输出通道
 86         FileOutputStream fos=new FileOutputStream(file3);
 87         FileInputStream fis=new FileInputStream(file1);
 88         FileInputStream fis1=new FileInputStream(file2);
 89         //把输入流存储到集合中,然后在从集合中读取
 90         ArrayList<FileInputStream> list=new ArrayList<FileInputStream>();
 91         list.add(fis);
 92         list.add(fis1);
 93         //准备一个数组
 94         byte[] buf=new byte[1024];
 95         int length=0;
 96         for (int i = 0; i < list.size(); i++) {
 97             FileInputStream fileInputStream=list.get(i);
 98             while((length=fileInputStream.read(buf))!=-1){
 99                 fos.write(buf, 0, length);
100             }
101             //关闭资源
102             fileInputStream.close();
103         }
104         fos.close();
105     }
106 }

原有文件

       

运行结果图

      

附录

 1 package com.dhb.file;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 import java.io.SequenceInputStream;
 8 import java.util.Enumeration;
 9 import java.util.Vector;
10 
11 /**
12  * @author DSHORE / 2018-7-16
13  *
14  */
15 //需求:mp3音乐的切割与合并
16 public class Demo22 {
17     public static void main(String[] args) throws IOException {
18         cutFile();
19         //mergeFile();
20     }
21     //合并mp3
22     public static void mergeFile() throws IOException{
23         //找到目标文件
24         File dir = new File("E:\MyMusic");
25         //通过目标文件找到所有mp3,然后把这些mp3添加到vector集合中
26         Vector<FileInputStream> vector = new Vector<FileInputStream>();
27         File[] file = dir.listFiles();
28         for (File file2 : file) {
29             if(file2.getName().endsWith(".mp3")){//获取以.mp3结尾的文件名
30                 vector.add(new FileInputStream(file2));//添加到vector集合中
31             }
32         }
33         Enumeration<FileInputStream> e = vector.elements();//返回此向量组件的枚举。即把上面集合中的元素给了e对象
34         SequenceInputStream sis = new SequenceInputStream(e);//建立序列流对象
35         //建立缓冲数组读取文件
36         byte[] buf = new byte[1024];
37         int length = 0;
38         FileOutputStream fos = new FileOutputStream(new File("MyMusic\MergeMusic\合并音乐-忘尘谷.mp3"));//建立输出字节流
39         while((length = sis.read(buf)) != -1){//读
40             fos.write(buf, 0, length);//写
41         }
42         fos.close();
43         sis.close();
44     }
45     //切割mp3
46     public static void cutFile() throws IOException{
47         File file = new File("E:\Music\忘尘谷.mp3");
48         //目标文件夹
49         File dir = new File("E:\MyMusic");
50         //建立数据输入通道
51         FileInputStream fis = new FileInputStream(file);
52         //建立缓冲数组
53         byte[] buf = new byte[1024*1024];//1024*1024 = 1MB,即:把该音乐切割成每份1MB的大小
54         int length = 0;
55       //边读边写
56 for (int i = 0; (length=fis.read(buf)) != -1; i++) {//读 (每次读1MB) 57 FileOutputStream fos = new FileOutputStream(new File(dir,"wcg"+i+".mp3"));//建立数据输出通道 58 fos.write(buf, 0, length);// 59 fos.close(); 60 } 61 fis.close(); 62 } 63 }

运行结果图

      

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/9306363.html

欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

原文地址:https://www.cnblogs.com/dshore123/p/9306363.html