• 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();
        }
        
        
    }
  • 相关阅读:
    k8s命令总结
    AttributeError: module 'enum' has no attribute 'IntFlag'
    程序员是如何思考的?
    2、Swift协程详解:用async函数替换回调
    7、Swift协程详解:异步函数的调度和GlobalActor
    6、Swift协程详解:属性隔离和Actor
    1、Swift协程详解:协程简介
    3、Swift协程详解:调用协程
    8、Swift协程详解:TaskLocal
    9、Swift协程详解:其他语言异步函数互调用
  • 原文地址:https://www.cnblogs.com/AllenRandolph/p/7017984.html
Copyright © 2020-2023  润新知