服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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

扫描关注微信公众号