<%@ page contentType="text/html; charset=gb2312"%> 用Java简单实现文件分割与合并
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
用Java简单实现文件分割与合并
     发布者: 发布时间:2007-02-13
 主要应用IO的RandomAccessFile(听说断点续传也是用它实现) 
 import java.io.*;

class Fen{
 String fileName;
 int 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;
  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(我设置的方便标识)");
}
}
 
(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
JavaScript在页面间数据传输的使用
怎样成为优秀的软件模型设计者?
JSP(Struts)避免Form重复提交的几种方案
用JSP实现类似资源管理器式的文件与目录的折叠(代码示例)
jsp中生成图片缩略图的代码
JSP图形验证码-汉字
 
最近评论:
        
冰封的往事!
wow power leveling,wow gold,WoW Gold,wow gold max(8741)
        
冰封的往事!
wow power leveling,wow gold,WoW Gold,wow gold max(7780)
        
飞舞的传奇!
传世私服,传世私服.传奇世界私服传奇世界私服,传世私服传世私服, 传奇世界私服传奇世界私服.传奇私服传奇私服. max(853)
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号