有时候一个视频文件或系统文件太大了,上传和下载可能会受到限制,这时可以用文件切割器把文件按大小切分为文件碎片,
等到要使用这个文件了,再把文件碎片合并成原来的文件即可。下面的代码实现了文件切割和文件合并功能。
一、切割文件
* 切割文件,按大小切割
* 把被切割的文件名和切割成的文件碎片数以键值对的形式写在配置文件中,
* 这要用到Properties集合
* 以便文件合并时可以读取并使用这些信息
1 public class SplitTest {
2
3 private static final int SIZE = 1024*1024;
4 public static void main(String[] args) throws IOException {
5 File file=new File("F:\\background.bmp");
6 splitFile(file);
7 }
8 public static void splitFile(File file) throws IOException{
9 //建立字节读取流
10 FileInputStream fis=new FileInputStream(file);
11 byte[] buf=new byte[SIZE];//建立1M的缓冲区
12 //建立碎片文件,所要放入的路径
13 File dir=new File("F:\\partFiles");
14 if(!dir.exists()){//目录不存在
15 dir.mkdirs();//则创建
16 }
17 //建立字节写入流
18 FileOutputStream fos=null;
19 int len=0;
20 int count=1;
21 while((len=fis.read(buf))!=-1){//从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
22 //在某些输入可用之前,此方法将阻塞。
23 fos=new FileOutputStream(new File(dir,(count++)+".part"));
24 fos.write(buf, 0, len);//将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
25 fos.close();
26 }
27 //把切割信息存储到配置文件中
28 fos=new FileOutputStream(new File(dir,count+".properties"));
29 Properties prop=new Properties();
30 prop.setProperty("partCount", (count-1)+"");
31 prop.setProperty("fileName", file.getName());
32 prop.store(fos, "split info in file");
33 fis.close();
34 fos.close();
35 }
36 }
二、合并文件
1 //合并文件
2 public class MergeFileTest {
3
4 public static void main(String[] args) throws IOException {
5 File dir=new File("F:\\partFiles");
6 mergeFiles(dir);
7 }
8 public static void mergeFiles(File file) throws IOException{
9 //从配置文件中获取信息
10 //先做健壮性判断 判断配置文件数量是否正确
11 File[] files=file.listFiles(new SuffixFilter(".properties"));
12 if(files.length!=1){
13 throw new RuntimeException("当前目录下没有配置文件或者配置文件不唯一");
14 }
15 //获取信息
16 File configFile=files[0];//获取配置文件对象
17 Properties prop=new Properties();
18 FileInputStream fis=new FileInputStream(configFile);
19 prop.load(fis);
20
21 String fileName=prop.getProperty("fileName");
22 int count=Integer.parseInt(prop.getProperty("partCount"));
23
24 //判断此目录下的文件碎片数是否正确
25 File[] partFiles=file.listFiles(new SuffixFilter(".part"));
26 if(partFiles.length!=count){
27 throw new RuntimeException("文件碎片数不正确,请重新下载");
28 }
29
30 ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();
31 //把读取流添加到集合中去
32 for(int i=0;i<count;i++){
33 al.add(new FileInputStream( partFiles[i]));
34 }
35 Enumeration<FileInputStream> en=Collections.enumeration(al);
36 //建立序列流
37 SequenceInputStream sis=new SequenceInputStream(en);
38
39 //建立写入流
40 FileOutputStream fos=new FileOutputStream(new File(file,fileName));
41 byte[] buf=new byte[1024];
42 int len=0;
43 while((len=sis.read(buf))!=-1){
44 fos.write(buf, 0, len);
45 }
46 sis.close();
47 fos.close();
48 }
49 }
1 public class SuffixFilter implements FilenameFilter { 2 private String suffix; 3 public SuffixFilter(String suffix) { 4 super(); 5 this.suffix = suffix; 6 } 7 @Override 8 public boolean accept(File arg0, String arg1) { 9 // TODO Auto-generated method stub 10 return arg1.endsWith(suffix); 11 } 12 13 }
不足之处:
1.没有图形化界面,不好操作。
2.应该把细化一些小的功能写成方法,而不是都写在一个大的方法中。