首页
论坛
图书
开发资料
在线文档
网址
下载
联系我们
 新闻│Java│JavaScript│Eclipse│Eclipse 英文│J2EE│J2ME│J2SE│JSP│Netbeans│Hibernate│JBuilder│Spring│Struts
站内搜索: 请输入搜索关键词

当前页面: 开发资料首页 → Java 专题 → 简单的文件分割与合并

简单的文件分割与合并

摘要: 简单的文件分割与合并

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="55" align="center" valign="top"> </td> </tr> <tr> <td height="20"><table cellspacing="0" cellpadding="10"> <tr> <td></td> </tr> </table> 主要应用IO的RandomAccessFile(听说断点续传也是用它实现)
import java.io.*;

class Fen{//分割文件

 String fileName;//待分割的文件名

 int size;//将大文件分成size大小的多个文件

 Fen(String fileName,String size){

  this.fileName = fileName;

  this.size = Integer.parseInt(size)*1024;

 }

 

  public void cut()throws Exception{

   int maxx = 0;

   File inFile = new File(fileName);

  

   int fileLength = (int)inFile.length();  //取得文件的大小

   int value;             //取得要分割的个数

  

   RandomAccessFile inn = new RandomAccessFile(inFile,"r");//打开要分割的文件

  

  

   value = fileLength/size;

  

   int i=0;

   int j=0;

  

   //根据要分割的数目输出文件

   for (;j< value;j++){

    File outFile = new File(inFile.getName()+j+"zzii");

    RandomAccessFile outt= new RandomAccessFile(outFile,"rw");

    maxx+=size;

    for (;i< maxx;i++){

     outt.write(inn.read());

    }

    outt.close();

   }

   File outFile = new File(inFile.getName()+j+"zzii");

   RandomAccessFile outt= new RandomAccessFile(outFile,"rw");

   for(;i< fileLength;i++){

    outt.write(inn.read());

   }

   outt.close();

   inn.close();

 }

}

 class He{ //合并文件

  String fileName;//将多个小文件合并成fileName

  String filterName;//小文件的扩展名

  

   He(String fileName,String filterName){

    this.fileName = fileName;

    this.filterName = filterName;

   }

  

  

   public void unite()throws Exception{

    String [] tt;

    File inFile = new File("."); //在当前目录下的文件

    File outFile = new File(fileName);  //取得输出名

    RandomAccessFile outt= new RandomAccessFile(outFile,"rw");

  

    //取得符合条件的文件名

    tt = inFile.list(new FilenameFilter(){

     public boolean accept(File dir,String name){

      String rr = new File(name).toString();

      return rr.endsWith(filterName);

     }

    });

    //打印出取得的文件名

    for (int i = 0;i< tt.length;i++){

     System.out.println(tt[i]);

    }

   

    //打开所有的文件再写入到一个文件里

    for(int i=0;i< tt.length;i++){

     inFile = new File(tt[i]);

     RandomAccessFile inn= new RandomAccessFile(inFile,"r");

     int c;

     while((c=inn.read())!=-1)

      outt.write(c);

    }

   

    outt.close();

   }

  }

 

public class test{

 public static void main(final String [] args)throws Exception{

 

  if(args.length==0){

   print();

   return;

  }

  if(args[0].equals("-c")){

   Fen cutt = new Fen(args[1],args[2]);

   cutt.cut();

  }

  else if (args[0].equals("-r")){

   He hee = new He(args[1],args[2]);

   hee.unite();

  }

  else

   print();

  

 }

 public static void print(){

  System.out.println("usage:\n分: java test -c file1 size(单位为K)\n合 java test -r file2 zzii(我设置的方便标识)");

}

}

运行结果:

C:\java>java  test

usage:

分: java test -c file1 size(单位为K)

合 java test -r file2 zzii(我设置的方便标识)

C:\java>java test -c ant.jar 100

C:\java>java test -r ant.jar zzii

ant.jar0zzii

ant.jar1zzii

ant.jar2zzii

ant.jar3zzii

ant.jar4zzii

ant.jar5zzii

ant.jar6zzii

ant.jar7zzii

ant.jar8zzii

ant.jar9zzii

ant.jar10zzii


</td> </tr> </table>

 


function TempSave(ElementID) { CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value); CommentsPersistDiv.save("CommentXMLStore"); } function Restore(ElementID) { CommentsPersistDiv.load("CommentXMLStore"); document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent"); } </td> </tr> <tr>


↑返回目录
前一篇: 用Look And Feel打造绚丽的界面外观!
后一篇: Java2,v5.0新特性实例

首页 | 全站 Sitemap | 联系我们 | 设为首页 | 收藏本站
版权所有 Copyright © 2006-2007, Java 编程资料牛鼻站, All rights reserved