IO流拆分以及合并文件

IO流拆分以及合并文件

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;
public class Demo9 {
    public static void main(String[] args) {
        spiltFile();
        MergeFile();
    }

    private static void MergeFile() {//合并文件
        FileInputStream fileInputStream=null;
        SequenceInputStream sequenceInputStream=null;
        FileOutputStream fileOutputStream=null;
        try {
            File file=new File("D:\\Temp","合集.mp3");
            fileOutputStream =new FileOutputStream(file);
            Vector<FileInputStream> vector=new Vector<>();
            for (int i = 1; i <=5 ; i++) {
                File file1=new File("D:\\Temp",i+".mp3");
                fileInputStream=new FileInputStream(file1);
              vector.add(fileInputStream);
            }
            Enumeration<FileInputStream> elements = vector.elements();
            sequenceInputStream=new SequenceInputStream(elements);
            int len=0;
            byte[] bytes=new byte[1024*8];
            while ((len=sequenceInputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes,0,len);
                fileOutputStream.flush();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (sequenceInputStream != null) {
                try {
                    sequenceInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream!= null) {
                try {
                  fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    private static void spiltFile() {//拆分文件
        RandomAccessFile in=null;
        RandomAccessFile out=null;
        RandomAccessFile out1=null;
        try{
            File file=new File("C:\\Users\\Administrator\\Desktop\\mp3\\许巍 - 蓝莲花.mp3");
             in=new RandomAccessFile(file,"rw");
            //System.out.println(file.length());
            long ls = file.length() /(1024*1024);
            for (int i = 1; i <=ls ; i++) {
                File file1=new File("D:\\temp",i+".mp3");
               out=new RandomAccessFile(file1,"rw");
               byte[] bytes=new byte[1024*1024];
               in.read(bytes);
               out.write(bytes,0,1024*1024);
               in.seek(in.getFilePointer());
            }
            File file2=new File("D:\\temp",ls+1+".mp3");
             out1=new RandomAccessFile(file2,"rw");
            int len=0;
            byte[] bytes=new byte[1024*8];
            while ((len=in.read(bytes))!=-1){
                out1.write(bytes,0,len);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (out1 != null) {
                try {
                    out1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
已标记关键词 清除标记
相关推荐
<p> 本门课程深入浅出的讲解了JavaIO中的高级部分内容,主要内容有: </p> <p> <span style="font-size:19px;">l<span style="font-size:9px;">  </span></span><span style="font-size:19px;">LineNumberReader</span> </p> <p> <span style="font-size:19px;">l<span style="font-size:9px;">  </span></span><span style="font-size:19px;">数据输入输出(DataInputStream和DataOutputStream)</span> </p> <p> <span style="font-size:19px;">l<span style="font-size:9px;">  </span></span><span style="font-size:19px;">内存操作</span> </p> <p>             字节内存操作 ByteArrayInputStream ByteArrayOutputStream  </p> <p>             字符内存操作 CharArrayReader CharArrayWrite </p> <p>                     字符串内存操作 StringReader  StringWriter </p> <p> <span style="font-size:19px;">l<span style="font-size:9px;">  </span></span><span style="font-size:19px;">打印(PrintStream 和 PrintWriter)</span> </p> <p> <span style="font-size:19px;">l<span style="font-size:9px;">  </span></span><span style="font-size:19px;">标准输入输出(System.in 和 System.out)</span> </p> <p> <span style="font-size:19px;">l<span style="font-size:9px;">  </span></span><span style="font-size:19px;">随机访问(RandomAccessFile)</span> </p> <p> <span style="font-size:19px;">l<span style="font-size:9px;">  </span></span><span style="font-size:19px;">合并(SequenceInputStream)</span> </p> <p> <span style="font-size:19px;">l<span style="font-size:9px;">  </span></span><span style="font-size:19px;">序列化和反序列化(ObjectOutputStream/ObjectInputStream)</span> </p> <p> <span style="font-size:19px;">l<span style="font-size:9px;">  </span></span><span style="font-size:19px;">Properties </span><span style="font-size:19px;">(集合</span><span style="font-size:19px;">+IO</span><span style="font-size:19px;">)</span> </p>
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页