科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道巧用Java语言简单实现文件的分割与合并

巧用Java语言简单实现文件的分割与合并

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

巧用Java语言简单实现文件的分割与合并

作者:baocl 来源:赛迪网技术社区 2007年11月16日

关键字: 合并 分割 java

  • 评论
  • 分享微博
  • 分享邮件

主要应用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:
分: java test -c file1 size(单位为K)
合 java test -r file2 zzii(我设置的方便标识)");
}
}

查看本文来源
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章