主要應用IO的RandomAccessFile(聽說斷點續(xù)傳也是用它實現(xiàn))
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; //取得要分割的個數(shù)
RandomAccessFile inn = new RandomAccessFile(inFile,"r");//打開要分割的文件
value = fileLength/size;
int i=0;
int j=0;
//根據要分割的數(shù)目輸出文件
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(我設置的方便標識)");
}
}
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請
點擊舉報。