Java 实现大文件的分割与合并


  1. package namespace;  
  2. import java.io.*;  
  3. public class Study  
  4. {  
  5.     private  static int length=1024;//可以设置文件在读取时一次读取文件的大小    
  6.       
  7.     /* 
  8.      * 文件的切割 
  9.      * String path   切割文件的路径 
  10.      * size      子文件的大小 
  11.      * */  
  12.     public static void  filesplit(String path,int size)throws Exception  
  13.     {  
  14.         if(path==null)  
  15.             throw new Exception("源文件不能为空...");  
  16.         File file=new File(path);  
  17.         if(!file.exists())  
  18.             throw new Exception("源文件不存在...");  
  19.         long num=file.length()%size==0?file.length()/size:file.length()/size+1;  
  20.         String list[]=new String[(int)num];//用于存放分割后的结果   
  21.         FileInputStream reader=new FileInputStream(file);  
  22.         long beginIndex=0,endIndex=0;  
  23.         int readcount=0;byte buffer[]=new byte[length];  
  24.         for(int i=0;i<num;i++)  
  25.         {  
  26.             list[i]=file.getAbsolutePath()+".depart"+i+".rmvb";  
  27.             FileOutputStream writer=new FileOutputStream(list[i]);  
  28.             endIndex=(endIndex+size)>file.length()?file.length():endIndex+size;  
  29.             for(;beginIndex<endIndex;)  
  30.             {  
  31.                 if(endIndex-beginIndex>=length) {  
  32.                  readcount=reader.read(buffer);  
  33.                  beginIndex+=readcount;  
  34.                  writer.write(buffer);  
  35.                 }else {  
  36.                     //下面的就不能直接读取1024个字节了,就要一个一个字节的读取了   
  37.                     for(;beginIndex<endIndex;beginIndex++)  
  38.                     {  
  39.                         writer.write(reader.read());  
  40.                     }  
  41.                     continue;  
  42.                 }  
  43.             }  
  44.             writer.close();  
  45.               
  46.         }  
  47.     }  
  48.     public static void union(String path,String newString)throws Exception  
  49.     {  
  50.         File file=new File(path);  
  51.         File list[]=file.listFiles();  
  52.         File newFile=new File(newString);  
  53.         byte buffer[]=new byte[1024];  
  54.         int readcount;  
  55.         if(!newFile.getParentFile().exists())  
  56.             throw new Exception("你合并的文件夹的不存在...");  
  57.         FileOutputStream writer=new FileOutputStream(newString);  
  58.         for(File f:list)  
  59.         {  
  60.             FileInputStream reader=new FileInputStream(f);  
  61.             while((readcount=reader.read(buffer))!=-1)  
  62.             {  
  63.                 writer.write(buffer);  
  64.             }  
  65.             reader.close();  
  66.         }  
  67.         writer.close();  
  68.     }  
  69.     public static void main(String args[])throws Exception  
  70.     {  
  71.      // filesplit("F:\\movie\\movie.rmvb",104857600);   
  72.         //union("F:\\movie\\","F:\\movie.rmvb");   
  73.     }  
  74. }  
  75. /* 
  76.  *                      文件的分割与合并 
  77.  *          1.文件的分割,原理是用输入流去读取文件,将读取规定大小的流再输出支指定的文件,直到整个把整个文件读取结束. 
  78.  *          2.文件合并,文件的合并原理与分割正好想反,就是把所有的文件都读取到一个输入流中,然后再把输入流中的东西全部输出到 
  79.  *                  同一个文件输出流中,这样就可以把分割的文件合并到一个文件中去了. 并且文件的大小和原来也会一样 .  
  80.  *          3.上面的程序我试着分割一个600多M 的电影,分割是成功了,但只是分割的第一个文件可以播放,而后面的几个文件都不可以播放,我也不知道为什么, 
  81.  *                  可能是视频文件里面有什么自定的格式吧...不过分割后再把所有的文件合并,合并后文件大小和之前一样,而且还可以插入,说明文件没有分割坏 
  82.  *                  至于单个的文件为什么不能插入,这个以后用到的时候再去研究..现在还是把Java的基础搞懂再说.....  
  83.  *                                                                                              2011.10.21 9:43 
  84.  *  *  *  
  85.  *  
  86.  *  
  87.  * */  

相关内容