zoukankan      html  css  js  c++  java
  • IO流_文件切割与合并(带配置信息)

    在切割文件的时候应该生成一个记录文件信息的文件,以便在以后合并文件的时候知道这个文件原来的文件名和记录文件切割完后生成了多少个切割文件

     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileOutputStream;
     4 import java.io.IOException;
     5 import java.util.Properties;
     6 
     7 public class SplitFileDemo {
     8     private static final int SIZE = 1024*1024;//1M=1024*1024个字节
     9 
    10     public static void main(String[] args) throws IOException {
    11         File file = new File("F:\jian.avi");
    12         splitFileDemo(file);
    13         
    14     }
    15     public static void splitFileDemo(File file) throws IOException {
    16         
    17         //用读取流关联一个文件
    18         FileInputStream fis = new FileInputStream(file);
    19         
    20         //定义一个1M的缓冲区
    21         byte[] buf = new byte[SIZE];
    22         
    23         //创建目的
    24         FileOutputStream fos = null;
    25         int count = 1;
    26         int len = 0;
    27         /*
    28          * 切割文件时,必须记录被切割文件的名称,以及切割出来碎片文件的个数,以便于合并
    29          * 这个信息为了进行简单的描述,使用键值对的方式,用到了properties对象
    30          * properties
    31          * */
    32         Properties prop = new Properties();
    33         
    34         File dir = new File("F:\partfiles");
    35         if(!dir.exists()){
    36             dir.mkdirs();
    37         }
    38         
    39         while((len = fis.read(buf))!=-1){
    40             fos = new FileOutputStream(new File(dir,(count++)+".part"));//扩展名可以依据软件的需求来定义,但不要是txt这些扩展名,因为切割完的文件不能直接读取
    41             fos.write(buf,0,len);
    42             fos.close();
    43         }
    44         //将被切割文件的信息保存到prop集合中
    45         prop.setProperty("partcount", count+"");
    46         prop.setProperty("filesname", file.getName());
    47         fos = new FileOutputStream(new File(dir,count+".properties"));
    48         //将prop集合中的数据存储到文件中
    49         prop.store(fos, "save file info");
    50         
    51         fos.close();
    52         fis.close();
    53     }
    54 }

    文件合并(将被切割了的文件合并回去)

      先定义一个过滤器:

    import java.io.File;
    import java.io.FilenameFilter;
    
    public class suffixFilter implements FilenameFilter {
        private String suffix;
        public suffixFilter(String suffix) {
            super();
            this.suffix = suffix;
        }
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(suffix);
        }
    }
     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileOutputStream;
     4 import java.io.IOException;
     5 import java.io.SequenceInputStream;
     6 import java.util.ArrayList;
     7 import java.util.Collections;
     8 import java.util.Enumeration;
     9 import java.util.Properties;
    10 
    11 public class MergeFile {
    12 
    13     public static void main(String[] args) throws IOException {
    14         File dir = new File("F:\partfiles");
    15         mergeFile(dir);
    16         
    17     }
    18     public static void mergeFile(File dir) throws IOException{
    19         
    20         
    21         //先拿取保存文件信息的那个配置文件对象============================
    22         File[] files = dir.listFiles(new suffixFilter(".properties"));//这里要过滤文件
    23         if(files.length!=1){
    24             throw new RuntimeException(dir+",该目录下没有properties扩展名的文件或者不唯一");
    25         }
    26         
    27         //记录配置文件对象
    28         File confile = files[0];
    29         
    30         //获取该文件中的信息=================================================
    31         Properties prop = new Properties();
    32         FileInputStream fis = new FileInputStream(confile);
    33         prop.load(fis);
    34         
    35         String filename = prop.getProperty("filesname");
    36         int count = Integer.parseInt(prop.getProperty("partcount"));
    37         
    38         //获取该目录下的所有碎片文件==========================================
    39         File[] partFiles = dir.listFiles(new suffixFilter(".part"));
    40         if(partFiles.length!=(count-1)){
    41             throw new RuntimeException("碎片文件不符合要求,个数应该是"+count+"个");
    42         }
    43         
    44         //将碎片文件和流对象关联并存储到集合中=========================================
    45         ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
    46         for (int i = 0; i < partFiles.length; i++) {
    47             al.add(new FileInputStream(partFiles[i]));
    48         }
    49         
    50         //将多个流合并为一个序列流=================================
    51         Enumeration<FileInputStream> en = Collections.enumeration(al);
    52         
    53         SequenceInputStream sis = new SequenceInputStream(en);
    54         
    55         FileOutputStream fos = new FileOutputStream(new File(dir,filename));
    56         byte[] buf = new byte[1024];
    57         int len = 0;
    58         while((len=sis.read(buf))!=-1){
    59             fos.write(buf,0,len);
    60         }
    61         
    62         sis.close();
    63         fos.close();
    64     }
    65 
    66 }
  • 相关阅读:
    Qt编译出错:“Cannot find file...... .pro."
    谈论如何有效地保护你的数据
    购书网
    VC非模态对话框创建和销毁
    在GUI程序中使用控制台的两种方法
    fatal error C1010: unexpected end of file while looking for precompiled header directive
    burp抓取手机包
    为firefox添加flash插件
    msfvenom生成linux后门
    kali下操作 Apache2
  • 原文地址:https://www.cnblogs.com/LO-ME/p/3596515.html
Copyright © 2011-2022 走看看