javaday53_IO流_文件分割与文件合并的综合应用

1.文件分割

package java_Studing_day53_IO流13practice;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;


/*
 * 文件切割器.
 *  
 */
public class SplitterFileDemo {


    private static final int SIZE = 1024*1024;


    public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
       File file=new File("F:\\FilesParts\\test.mp4");
      // splitFile(file);
       splitFile_2(file);
    }



//方法二,高级操作,切割的过程中将切割前的信息记录到本地,以便于合并
    public static void splitFile_2(File file) throws IOException {

//用读取流关联源文件
FileInputStream fis=new FileInputStream(file);

//定义一个1M的缓冲区
byte [] buf=new byte[SIZE];

//创建目的
FileOutputStream fos=null;

int len=0;
//改变新创建文件的名字
int count = 1;

//封装分割后文件的存储位置
File dir=new File("f:\\FilesParts");

if(!dir.exists())
   dir.mkdirs();


while((len=fis.read(buf))!=-1){
     
   fos=new FileOutputStream(new File(dir,(count++)+".part"));
  fos.write(buf, 0, len); 
  fos.close();
  }
/*
* 切割文件时必须要记录着被切割文件的名称以及切割出来的碎片文件的个数,以方便与合并
* 这个信息为了能简单的描述,所以使用键值对的方式,用到了Properties

*/
Properties prop =new Properties();

//将被切割文件的信息保存到一个文件中
prop.setProperty("partcount", count+""); 
prop.setProperty("filename", file.getName());

//将
fos=new FileOutputStream(new File(dir,count+".properties"));
        
//将properties中的信息保存到本地文件
prop.store(fos, "rawfile info");

 fis.close();
          fos.close();
         //退出时删除源文件
          file.deleteOnExit();
    }    


    


    
 //方法一,简单切割,纯切割   
    
    public static void splitFile(File file) throws IOException{

//用读取流关联源文件
FileInputStream fis=new FileInputStream(file);

//定义一个1M的缓冲区
byte [] buf=new byte[SIZE];

//创建目的
FileOutputStream fos=null;

int len=0;
//改变新创建文件的名字
int count = 1;

//封装分割后文件的存储位置
File dir=new File("f:\\FilesParts");

if(!dir.exists())
   dir.mkdirs();


while((len=fis.read(buf))!=-1){
     
   fos=new FileOutputStream(new File(dir,(count++)+".part"));
  fos.write(buf, 0, len);
  }
          fis.close();
          fos.close();
    }    
}



2.文件合并.

package java_Studing_day53_IO流13practice;



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.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;


public class MergeFileDemo {


    public static void main(String[] args) throws IOException {
// 测试


File dir=new File("f:\\FilesParts");
//mergeFile(dir);
mergeFile_2(dir);
    }
/*
 * 方法二,高级操作,纪录文件分割前的信息到本地
 */
    
    public static void mergeFile_2(File dir) throws IOException {

/**获取指定目录下的配置文件对象==========================================================*/
//对指定路径下的文件进行过滤,获取.properties的配置文件
File[] files=dir.listFiles(new SuffixFilter(".properties"));

//判断获取到的配置文件否存在/个数
if(files.length!=1)
   throw new RuntimeException(dir+"目录下没有 .Properties拓展名的文件或者文件不唯一");

//将唯一的配置文件中封装成file对象便于操作
File config=files[0];

/**获取配置文件信息=============================================================*/
//加载该配置文件
Properties prop=new Properties();
FileInputStream fis=new FileInputStream(config);
prop.load(fis);
fis.close();

//从配置文件中获取分割前文件的名称和分割后碎片文件的个数
String filename=prop.getProperty("filename");
int partnumber=Integer.parseInt(prop.getProperty("partcount"));

/**获取该目录下的所有碎片文件个数====================================================*/
//获取指定目录下的碎片文件后缀名为.jorian的文件.
File[] partFiles=dir.listFiles(new SuffixFilter(".part"));

//判断获取到的碎片文件个数和配置信息中的文件格式不相等
if(partFiles.length!=(partnumber-1)){
   throw new RuntimeException("碎片文件个数不符合要求");
}

/**将碎片文件用流关联起来并存储到集合中=====================================================*/
//创建一个流容器
ArrayList<FileInputStream> al1=new ArrayList<FileInputStream>();
//将每一个碎片文件用输入流关联起来
for (int x=0;x<=partFiles.length-1;x++){
   
   al1.add(new FileInputStream(partFiles[x]));
   
}

/**将多个流合并成一个序列流=======================================================*/
//拿到流容器的枚举
Enumeration<FileInputStream> en=Collections.enumeration(al1);
//将所有的碎片文件流合并,序列化
SequenceInputStream sis=new SequenceInputStream(en);

//打开一个输出流并关联文件,位置为碎片文件所在目录,文件名为源文件名
FileOutputStream fos=new FileOutputStream(new File(dir,filename));


/**读写操作===================================================================*/
byte [] buf=new byte[2048];
int len=0;
while((len=sis.read(buf))!=-1){
   fos.write(buf, 0, len);
   
}
sis.close();
fos.close();

    }


/*
 * 
 * 方法一,初级操作,学习原理
 */
    public static void mergeFile(File dir) throws IOException{

//创建容器将流装起来
ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();

//遍历指定目录下的四个文件
        for(int x=1;x<=4;x++){
          //将四个文件开流,封装成流对象存到容器中
            al.add(new FileInputStream(new File(dir,x+".part")));
        }
        //取得容器的Enumeration,枚举
        Enumeration<FileInputStream> en=Collections.enumeration(al);
       
        //将多个流合并
SequenceInputStream sis=new SequenceInputStream(en);
//将输出路径封装成对象,并用流连接
FileOutputStream fos=new FileOutputStream(new File(dir,"merge.mp4"));

//写出到指定文件中,完成碎片合并
byte[] buf=new byte[1024];
int len=0;

while((len=sis.read(buf))!=-1){
   fos.write(buf,0,len);
   
}
sis.close();
fos.close();
    }
    
    
    

}

3.文件过滤器,获取指定拓展名的文件

package java_Studing_day53_IO流13practice;


import java.io.File;
import java.io.FilenameFilter;


public class SuffixFilter implements FilenameFilter {


    
    private String suffix;
   
    public SuffixFilter(String suffix) {
super();
this.suffix = suffix;
    }
    public boolean accept(File dir, String name) {
    // TODO Auto-generated method stub
    return name.endsWith(suffix);
       }
}


猜你喜欢

转载自blog.csdn.net/weixin_38023579/article/details/72452293
今日推荐