当前位置: 首页 > 图文教程 > 认证考试 > java认证 > 用Java简单实现文件分割与合并的事例

java认证
浅谈Java开发中的设计模式
Java基础教程:常见问题解答
写给在Java和.net中徘徊的新手
作为一个Java程序员 你应该会什么
在Java语言中如何使用This关键字
如何将各种数据库连接起来?
eclipse+myeclipse配置环境
使用ADO.NET Entity Framework构建数据访问层
Java面向对象设计的原则
程序员必读:JavaBean规范
Grails与遗留数据库
世上最昂贵的JS代码
用java编写1-6数字的main函数
详解Java内存机制(堆与栈)的分配
代码保镖:Java代码混淆器
junit单元测试使用方法
J2EE学习总结:思维方式和理念
Java5.0的元注解(meta-annotations)
java能否继续保持在开发领域的领导地位?
Java教程:如何使用Annotation

java认证 中的 用Java简单实现文件分割与合并的事例


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-04   浏览: 65 ::
收藏到网摘: n/a

主要应用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    File outFile = new File(inFile.getName()+j+"zzii");
    RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
    maxx+=size;
    for (;i     outt.write(inn.read());
    }
    outt.close();
   }
   File outFile = new File(inFile.getName()+j+"zzii");
   RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
   for(;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     System.out.println(tt[i]);
    }
   
    //打开所有的文件再写入到一个文件里
    for(int i=0;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(我设置的方便标识)");
}
}