用Java簡(jiǎn)單實(shí)現文件分割與合并
主要應用IO的RandomAccessFile(聽(tīng)說(shuō)斷點(diǎn)續傳也是用它實(shí)現) k瞉烏?鵷?
import java.io.*; \ qX駒?
簭? -蘆?
class Fen{ 珨u曱}頌瓑
String fileName; @猇C*^
int size; cT"{點(diǎn)K?
|t撱斕 F?
Fen(String fileName,String size){ 趩? ?8M
this.fileName = fileName; 頓檨$A<郩
this.size = Integer.parseInt(size)*1024; 菇?%鍌帷L
} OxP垰?
h検輞
public void cut()throws Exception{ 躁嚳X?鰄0
int maxx = 0; 讚礙}v?
File inFile = new File(fileName); E瑸芺綒?
選H嫙?
int fileLength = (int)inFile.length(); //取得文件的大小 赨ms莂莀
int value; //取得要分割的個(gè)數 H_s??銁n
2放e|N
RandomAccessFile inn = new RandomAccessFile(inFile,"r");//打開(kāi)要分割的文件 萊峛I劇飥
赑蝞軙H媂?
.pd愀aNv
value = fileLength/size; a€姝$vi
>悼S?鰖O0
int i=0; 9tF粃鉉
int j=0; 頮?H玸氎
晉>?p?
//根據要分割的數目輸出文件 昃鉸X[
for (;j File outFile = new File(inFile.getName()+j+"zzii"); 鈧擴)p璅?
RandomAccessFile outt= new RandomAccessFile(outFile,"rw"); N?2wK沈?
maxx+=size; 豸 狚?6垻
for (;i outt.write(inn.read()); 3?G簛k|1?
} 衛?錚S??
outt.close(); {eU?‘貰
} D?艻"?
File outFile = new File(inFile.getName()+j+"zzii"); 耕m€瀵 廩?
RandomAccessFile outt= new RandomAccessFile(outFile,"rw"); Z`?/O僴玒
for(;i >?sf?
outt.write(inn.read()); n穹)?遴
} B乜?祃?
outt.close(); J魋?|x2杴
xex櫬?vY
inn.close(); }樅kA棹宑
} 勸0清紗
} ~?Wī
1;@~濤醮調
*w? y
class He{ _h湄譪銲?
String fileName; N<K陡x?Q?
String filterName; ?In?<?
橅5eA‘銚?
He(String fileName,String filterName){ DP旕瑒??
this.fileName = fileName; #"=z瑨T?
this.filterName = filterName; 鋦q丂亡A?
} Z樗檰慭譋
H?唓~啄
佤#n%1
public void unite()throws Exception{ ‘J庎A7
String [] tt; _7-:‘??
File inFile = new File("."); //在當前目錄下的文件 y?YvZWd
File outFile = new File(fileName); //取得輸出名 w醯U慽鐸?
RandomAccessFile outt= new RandomAccessFile(outFile,"rw"); 櫠(鹙祪y?
枓炍T铚:!
//取得符合條件的文件名 g?巷A苿z
tt = inFile.list(new FilenameFilter(){ 囉囷舔蜢
public boolean accept(File dir,String name){ r耜;?魦
String rr = new File(name).toString(); C?專(zhuān)Y
return rr.endsWith(filterName); 矟Ryb沌鉔
} T?鸐忢?o
}); ?褒a瓑
//打印出取得的文件名 輊荂-{YD
for (int i = 0;i System.out.println(tt); 飝WE弼?
} 靿N鯊6#?
r傛)鉻h
//打開(kāi)所有的文件再寫(xiě)入到一個(gè)文件里 j4:?]Q繑
for(int i=0;i inFile = new File(tt); €"0?Pr
RandomAccessFile inn= new RandomAccessFile(inFile,"r"); G溫ngm燛
int c; 呵Gj慣Z?
while((c=inn.read())!=-1) 3???DZ
outt.write(c); #U椀]洙 Su
} 駔瓙馘?ǎ
蹊葷3蹀趖W
outt.close(); 巔e彥鱫??
} l+],酞e
} 0?.?稬?
]嫵RM繕 縭
識瓍7? J?
public class test{ yT闌n>
public static void main(final String [] args)throws Exception{ Ⅱm?eo塤
晾な8穯乗
if(args.length==0){ TKㄩp?)
print(); ク????
return; 溫勵BB?
} g?4侱q礌
if(args[0].equals("-c")){ 蠢齞吢Y?
Fen cutt = new Fen(args[1],args[2]); L濵闧J??
cutt.cut(); lt;? L澐放
} Asf; 煌] ?
else if (args[0].equals("-r")){ 0ヲ9╨梙煼
He hee = new He(args[1],args[2]); ZZMé枔?
hee.unite(); 問(wèn)7;^*i
} 媜a轄靜
else #si浽?
print(); B.囉<<%
r芆樼窄觚?
} 5憝C.M檼?
濰`?>僓
public static void print(){ p咚藀>c埄?
System.out.println("usage:\n分: java test -c file1 size(單位為K)\n合 java test -r file2 zzii(我設置的方便標識)"); 4"f%ov韑?
} 炋o廄mN?}
}
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。