zoukankan      html  css  js  c++  java
  • java笔记之IO详解——序列流

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

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

    需求:把a.txt与b.txt 文件的内容合并。写入到c.txt。

    import java.io.File;
    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; public class test01 { public static void main(String[] args) throws IOException { File file01=new File("F:/a.txt"); File file02=new File("F:/b.txt"); File out03=new File("F:/c.txt"); FileInputStream fileInputStream01=new FileInputStream(file01); FileInputStream fileInputStream02=new FileInputStream(file02); FileOutputStream fileOutputStream =new FileOutputStream(out03); ArrayList
    <FileInputStream> list=new ArrayList<FileInputStream>();//将其添加到Arraylist集合中, list.add(fileInputStream01); list.add(fileInputStream02); int len=0; byte buff[]=new byte[1024]; for (int i = 0; i < list.size(); i++) { FileInputStream fileInputStream=list.get(i);//取出对象 while ((len=fileInputStream.read(buff))!=-1) {//读取内容 fileOutputStream.write(buff, 0, len);//将字节写出 } fileInputStream.close(); } fileOutputStream.close(); } }
    使用SequenceInputStream合并文件。

        public static void merge2() throws IOException{
            //找到目标文件
            File inFile1 = new File("F:\a.txt");
            File inFile2 = new File("F:\b.txt");
            File outFile = new File("F:\c.txt");
            //建立数据的输入输出通道
            FileOutputStream fileOutputStream = new FileOutputStream(outFile);
            
            FileInputStream fileInputStream1 = new FileInputStream(inFile1);
            FileInputStream fileInputStream2 = new FileInputStream(inFile2);
            //建立序列流对象
            SequenceInputStream inputStream = new SequenceInputStream(fileInputStream1,fileInputStream2);
            byte[] buf = new byte[1024];
            int length = 0 ; 
            
            while((length = inputStream.read(buf))!=-1){
                fileOutputStream.write(buf,0,length);
            }
            //关闭资源
            inputStream.close();
            fileOutputStream.close();
    
        }

    此时的SequenceInputStream 只能处理2个,当处理多个时,除了第一种方法,还有我们前面学过的Vector集合,

        public static void combine03() throws IOException{
            File file01=new File("F:/a.txt");
            File file02=new File("F:/b.txt");
            File file03=new File("F:/c.txt");
            File out03=new File("F:/d.txt");
            FileInputStream fileInputStream01=new FileInputStream(file01);
            FileInputStream fileInputStream02=new FileInputStream(file02);
            FileInputStream fileInputStream03=new FileInputStream(file03);
            FileOutputStream fileOutputStream =new FileOutputStream(out03);
            Vector< FileInputStream> vector=new Vector<FileInputStream>();
            vector.add(fileInputStream01);
            vector.add(fileInputStream02);
            vector.add(fileInputStream03);
            Enumeration<FileInputStream> e=vector.elements();
            SequenceInputStream sequenceInputStream = new SequenceInputStream(e);
            
            //读取文件数据
            byte[] buf = new byte[1024];
            int length = 0; 
            
            while((length = sequenceInputStream.read(buf))!=-1){
                fileOutputStream.write(buf,0,length);
            }
            
            //关闭资源
            sequenceInputStream.close();
            fileOutputStream.close();
            
            
        }

     将一个MP3文件进行切割并和并,切割合并不影响播放

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.SequenceInputStream;
    import java.util.Enumeration;
    import java.util.Vector;
    
    /*
     
     需求: 把一首mp3先切割成n份,然后再把这些文件合并起来。
     
     */
    
    
    public class Demo2 {
    
        public static void main(String[] args) throws IOException {
    //        cutFile();
            mergeFlile();
        }
        
        //合并
        public static void mergeFlile() throws IOException{
            //找到目标文件
            File dir = new File("F:\music");
            //通过目标文件夹找到所有的MP3文件,然后把所有的MP3文件添加到vector中。
            Vector<FileInputStream> vector = new Vector<FileInputStream>();
            File[] files = dir.listFiles();
            for(File file : files){
                if(file.getName().endsWith(".mp3")){
                    vector.add(new FileInputStream(file));
                }
            }
            //通过Vector获取迭代器
            Enumeration<FileInputStream> e = vector.elements();
            //创建序列流
            SequenceInputStream inputStream = new SequenceInputStream(e);
            //建立文件的输出通道
            FileOutputStream fileOutputStream = new FileOutputStream("F:\合并.mp3");
            //建立缓冲数组读取文件
            byte[] buf = new byte[1024];
            int length = 0 ; 
            while((length =  inputStream.read(buf))!=-1){
                fileOutputStream.write(buf,0,length);
            }
            //关闭资源
            fileOutputStream.close();
            inputStream.close();
            
        }
        
        
        
        //切割MP3
        public static void cutFile() throws IOException{
            File file = new File("F:\美女\1.mp3");
            //目标文件夹
            File dir = new File("F:\music");
            //建立数据的输入通道
            FileInputStream fileInputStream = new FileInputStream(file);
            //建立缓冲数组读取
            byte[] buf = new byte[1024*1024];
            int length = 0;
            for(int i = 0 ;  (length = fileInputStream.read(buf))!=-1 ; i++){
                FileOutputStream fileOutputStream =    new FileOutputStream(new File(dir,"part"+i+".mp3"));
                fileOutputStream.write(buf,0,length);
                fileOutputStream.close();
            }
            //关闭资源
            fileInputStream.close();
        }
        
        
    }
  • 相关阅读:
    各国语言缩写列表,各国语言缩写-各国语言简称,世界各国域名缩写
    How to see log files in MySQL?
    git 设置和取消代理
    使用本地下载和管理的免费 Windows 10 虚拟机测试 IE11 和旧版 Microsoft Edge
    在Microsoft SQL SERVER Management Studio下如何完整输出NVARCHAR(MAX)字段或变量的内容
    windows 10 x64系统下在vmware workstation pro 15安装macOS 10.15 Catelina, 并设置分辨率为3840x2160
    在Windows 10系统下将Git项目签出到磁盘分区根目录的方法
    群晖NAS(Synology NAS)环境下安装GitLab, 并在Windows 10环境下使用Git
    使用V-2ray和V-2rayN搭建本地代理服务器供局域网用户连接
    windows 10 专业版安装VMware虚拟机碰到的坑
  • 原文地址:https://www.cnblogs.com/AllenRandolph/p/7017984.html
Copyright © 2011-2022 走看看