主要應用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(我設置的方便標識)");
}
}