เมื่อเร็ว ๆ นี้เพื่อแก้ไขชื่อไฟล์ของละครอเมริกันที่ชื่นชอบจำนวนมากฉันเขียนวิดเจ็ตโดยใช้สวิง รหัสคือส่วนการประมวลผลไฟล์ เนื้อหาเฉพาะมีดังนี้
แพ็คเกจ Datei.steuern; นำเข้า java.io.bufferedreader; นำเข้า java.io.file; นำเข้า java.io.fileinputstream; นำเข้า java.io.filenotfoundexception; นำเข้า java.io.fileoutputstream; นำเข้า java.io.filewriter; java.io.randomaccessfile; นำเข้า java.nio.bytebuffer; นำเข้า java.nio.channels.filechannel; นำเข้า java.util.arraylist; นำเข้า java.util.arrays; นำเข้า java.util.collections; java.util.logging.logger; /** * * @author s.watson */คลาสสาธารณะ filetools {public filetools () {}/** * ไดเรกทอรีไฟล์หลบหนี formatPath * * @param Path * @return */รูปแบบสตริงคงที่ (เส้นทางสตริงกลับ) } / ** * พา ธ ไฟล์ CombinePath ผสาน * * @param eins * @param zwei * @return * / สตริงคงที่ส่วนตัว combinepath (String eins, zwei zwei) {string dori = ""; eins = null == eins? "": FormatPath (EINS); zwei = null == zwei? "": FormatPath (Zwei); if (! eins.endswith ("/") && zwei.indexof ("/")! = 0) {dori = eins + "/" + zwei; } else {dori = (eins + zwei) .replaceall ("//", "/"); } return dori; } / ** * list2array list array array * * @param list * @return * / สตริงคงที่ส่วนตัว [] list2array (รายการรายการ) {string array [] = (string []) list.toarray (สตริงใหม่ [list.size ()]); อาร์เรย์กลับ; } / ** * CP Copy File * * @Param Source * @Param ปลายทาง * @param loop * @return * / รายการคงที่สาธารณะ <file> cp (แหล่งที่มาของสตริง, สตริงปลายทาง, boolean loop) {รายการ <file> list = new ArrayList (); ลอง {ไฟล์ srcfile = ไฟล์ใหม่ (แหล่งที่มา); ไฟล์ desfile = ไฟล์ใหม่ (ปลายทาง); list.addall (cp (srcfile, desfile, loop)); } catch (exception ex) {j2log (null, null, ex); } return list; } / ** * CP Copy File * * @Param Source * @param ปลายทาง * @param loop * @return * / รายการคงที่สาธารณะ <file> cp (แหล่งที่มาไฟล์, ปลายทางไฟล์, boolean loop) {รายการ <file> list = new ArrayList (); ลอง {ถ้า (! source.exists () || source.isdirectory ()) {โยน filenotfoundexception ใหม่ (); } list.add (CP (แหล่งที่มาปลายทาง)); if (loop) {string [] subfile = source.list (); สำหรับ (สตริง subpath: subfile) {string src = combinePath (source.getPath (), subpath); // เส้นทางไฟล์ต้นฉบับของ Subfile String des = combinePath (destination.getPath (), subPath); // เส้นทางเป้าหมายของไฟล์ subfile subfile = ไฟล์ใหม่ (SRC); if (subfile.isfile ()) {list.add (cp (src, des)); } อื่นถ้า (subfile.isdirectory () && loop) {list.addall (cp (src, des, loop)); }}}}} catch (Exception Ex) {j2log (null, null, ex); } return list; } / ** * ไฟล์คัดลอกไฟล์เดียว CP * * @Param Source * @Param ปลายทาง * @return * / ไฟล์คงที่สาธารณะ cp (แหล่งที่มาสตริง, ปลายทางสตริง) {ไฟล์ desfile = null; ลอง {ไฟล์ srcfile = ไฟล์ใหม่ (แหล่งที่มา); desfile = ไฟล์ใหม่ (ปลายทาง); desfile = cp (srcfile, desfile); } catch (exception ex) {j2log (null, null, ex); } return desfile; } / ** * ไฟล์คัดลอกไฟล์เดียว CP * * @Param Source * @Param ปลายทาง * @return * / ไฟล์คงที่ไฟล์ cp (แหล่งที่มาไฟล์, ปลายทางไฟล์) {fileInputStream ใน = null; fileOutputStream out = null; FileChannel Inc = null; fileChannel outc = null; ลอง {ถ้า (! source.exists () || source.isdirectory ()) {โยน filenotfoundexception ใหม่ (); } if (source.getPath (). เท่ากับ (ปลายทาง. getPath ())) {return source; } Long AllBytes = DU (แหล่งที่มา, เท็จ); if (! destination.exists ()) {destination.createnewfile (); } ใน = ใหม่ fileInputStream (source.getPath ()); out = ใหม่ fileOutputStream (ปลายทาง); inc = in.getChannel (); outc = out.getChannel (); Bytebuffer bytebuffer = null; ความยาวยาว = 2097152; // ขนาดพื้นฐาน, ค่าเริ่มต้นยาว 2m ยาว _2m = 2097152; ในขณะที่ (inc.position () <inc.size ()) {ถ้า (allbytes> (64 * ความยาว)) {// ถ้าขนาดไฟล์มากกว่า 128m แคชจะเปลี่ยนเป็นความยาว 64m = 32 * _2m; } อื่นถ้า (AllBytes> (32 * ความยาว)) {// ถ้าขนาดไฟล์มากกว่า 64m แคชจะเปลี่ยนเป็นความยาว 32m = 16 * _2m; } อื่นถ้า (AllBytes> (ความยาว 16 *)) {// ถ้าขนาดไฟล์มากกว่า 32m แคชจะเปลี่ยนเป็นความยาว 16 ม. = 8 * _2m; } อื่นถ้า (allbytes> (8 * ความยาว)) {// ถ้าขนาดไฟล์มากกว่า 16m แคชจะเปลี่ยนเป็นความยาว 8m = 4 * _2m; } อื่นถ้า (allbytes> (4 * ความยาว)) {// ถ้าขนาดไฟล์มากกว่า 8m แคชจะเปลี่ยนเป็นความยาว 4m = 2 * _2m; } อื่นถ้า (AllBytes> (2 * ความยาว)) {// ถ้าขนาดไฟล์มากกว่า 4m แคชจะเปลี่ยนเป็น 2m ความยาว = _2m; } อื่นถ้า (allbytes> (ความยาว)) {// ถ้าขนาดไฟล์มากกว่า 2m แคชจะเปลี่ยนเป็นความยาว 1m = _2m/2; } อื่นถ้า (AllBytes <ความยาว) {// ถ้าไฟล์มีขนาดเล็กกว่าขนาดฐานความยาวเอาต์พุตโดยตรง = AllBytes; } AllBytes = Inc.Size () - Inc.Position (); bytebuffer = bytebuffer.allocatedirect ((int) ความยาว); inc.read (bytebuffer); bytebuffer.flip (); outc.write (bytebuffer); outc.force (เท็จ); }} catch (exception ex) {j2log (null, null, ex); } ในที่สุด {ลอง {ถ้า (null! = inc) {inc.close (); } if (null! = outc) {outc.close (); } if (null! = in) {in.close (); } if (null! = out) {out.close (); }} catch (exception ex) {j2log (null, null, ex); }} ส่งคืนปลายทาง; } / ** * เปลี่ยนชื่อไฟล์เปลี่ยนชื่อ * * @param filepath * @param จาก * @param เป็น * @return * / การเปลี่ยนชื่อไฟล์คงที่สาธารณะ (สตริง filepath, สตริงจาก, สตริง) {ไฟล์ newfile = null; ลอง {ไฟล์ oldFile = ไฟล์ใหม่ (combainPath (filePath, จาก)); newFile = ไฟล์ใหม่ (combainPath (filepath, to)); เปลี่ยนชื่อ (Newfile, OldFile); } catch (exception ex) {j2log (null, null, ex); } ส่งคืน Newfile; } / ** * เปลี่ยนชื่อไฟล์เปลี่ยนชื่อ * * @param เป็น * @param จาก * @return * / การเปลี่ยนชื่อไฟล์สแตติกสาธารณะ (ไฟล์จาก, ไฟล์เป็น) {ลอง {String newpath = to.getPath (); String oldPath = from.getPath (); if (! oldpath.equals (newpath)) {ถ้า (! to.exists ()) {from.renameto (ถึง); }}} catch (Exception Ex) {j2log (null, null, ex); } กลับไปที่; } / ** * MV Move File * * @param filename * @param แหล่งที่มา * @param ปลายทาง * @param cover * / โมฆะคงที่สาธารณะ MV (ชื่อไฟล์สตริง, แหล่งสตริง, ปลายทางสตริง, ฝาครอบบูลีน) {ลอง {ถ้า (! source.equals (ปลายทาง)) ไฟล์ newfile = ไฟล์ใหม่ (combainpath (ปลายทาง, ชื่อไฟล์)); MV (OldFile, Newfile, ปก); }} catch (exception ex) {j2log (null, null, ex); }} / ** * MV Move File * * @Param Source * @Param ปลายทาง * @Param Cover * / โมฆะสาธารณะคงที่ MV (แหล่งที่มาของสตริง, ปลายทางสตริง, ฝาครอบบูลีน) {ลอง {ถ้า (! source.equals (ปลายทาง)) {ไฟล์ oldfile = ไฟล์ใหม่ (แหล่งที่มา); ไฟล์ newfile = ไฟล์ใหม่ (ปลายทาง); MV (OldFile, Newfile, ปก); }} catch (exception ex) {j2log (null, null, ex); }} / ** * MV Move File * * @Param Source * @Param ปลายทาง * @Param Cover * / โมฆะสาธารณะคงที่ MV (แหล่งที่มาของไฟล์, ปลายทางไฟล์, ฝาครอบบูลีน) {ลอง {ถ้า (! source.exists ()) {โยน filenotfoundException ใหม่ (); } stringBuilder filename = new StringBuilder (source.getName ()); if (! cover && source.getPath (). Equals (destination.getPath ())) {ถ้า (filename.indexof (".")> 0) {filename.insert (filename.lastindexof ("."), "_copy"); } else {filename.append ("_ copy"); } cp (แหล่งที่มาไฟล์ใหม่ (combainpath (source.getParent (), filename.toString ()))); } else {source.renameto (ปลายทาง); }} catch (exception ex) {j2log (null, null, ex); }} / ** * ส่วนขยายรับข้อมูลส่วนขยายไฟล์ * * @param filepath * @param filename * @return * / สตริงคงที่ส่วนตัว [] ส่วนขยาย (สตริง filepath, ชื่อไฟล์สตริง) {สตริง [] ส่วนขยาย = {}; ลอง {String fullPath = combinePath (filePath, ชื่อไฟล์); ไฟล์ file = ไฟล์ใหม่ (FullPath); ส่วนขยาย (ไฟล์); } catch (exception ex) {j2log (null, null, ex); } return extension; } / ** * ส่วนขยายรับข้อมูลส่วนขยายไฟล์ * * @param fullPath * @return * / สตริงคงที่ส่วนตัว [] ส่วนขยาย (String FullPath) {String [] Extension = {}; ลอง {file file = ไฟล์ใหม่ (fullPath); ส่วนขยาย (ไฟล์); } catch (exception ex) {j2log (null, null, ex); } return extension; } / ** * ส่วนขยายรับข้อมูลส่วนขยายไฟล์ * * @param ไฟล์ * @return * / สตริงคงที่ส่วนตัว [] ส่วนขยาย (ไฟล์ไฟล์) {string [] ส่วนขยาย = {}; ลอง {ถ้า (file.isfile ()) {string filename = file.getName (); if (filename.lastindexof (".")> = 0) {int lastIndex = filename.lastindexof ("."); ส่วนขยาย [0] = string.valueof (LastIndex); // index of "." ส่วนขยาย [1] = filename.substring (LastIndex + 1); // ส่วนขยายส่วนขยาย [2] = filename.substring (0, LastIndex); // ชื่อไฟล์}}} catch (Exception Ex) {J2log (null, null, ex); } return extension; } / ** * ls traversing ไฟล์ * * @param filepath * @param loop * @return * / รายการคงที่สาธารณะ <file> ls (สตริง filepath, boolean loop) {รายการ <file> list = new ArrayList (); ลอง {file file = ไฟล์ใหม่ (filePath); list.addall (ls (ไฟล์, วน)); } catch (exception ex) {j2log (null, null, ex); } return list; } / ** * ls traversing ไฟล์ * * @param ไฟล์ * @param loop * @return * / รายการคงที่สาธารณะ <file> ls (ไฟล์ไฟล์, ลูบบูลีน) {รายการ <file> list = newraylist (); ลอง {list.add (ไฟล์); if (! file.isdirectory ()) {list.add (ไฟล์); } อื่นถ้า (file.isdirectory ()) {ไฟล์ [] sublist = file.listfiles (); SUBLIST = FILESSORT (SUBLIST, TRUE); สำหรับ (ไฟล์ subfile: sublist) {if (subfile.isdirectory () && loop) {list.addall (ls (subfile.getPath (), ลูป)); } else {list.add (subfile); }}}} catch (Exception Ex) {j2log (null, null, ex); } return list; } / ** * การเรียงลำดับไฟล์ filessort (คำสั่งซื้อเริ่มต้นขึ้น) * * @param parentpath * @param sublist * @return * / ไฟล์คงที่ส่วนตัว [] filessort (ไฟล์ [] infiles, boolean asc) {list <string> ไฟล์ = new ArrayList (); รายการ <String> dirs = new ArrayList (); สำหรับ (ไฟล์ subfile: infiles) {if (subfile.isdirectory ()) {dirs.add (subfile.getPath ()); } อื่นถ้า (subfile.isfile ()) {files.add (subfile.getPath ()); }} สตริง [] filearray = {}; if (files.size ()> 0) {filearray = list2array (ไฟล์); array.sort (Filearray); if (! asc) {arrays.sort (filearray, collections.reverseorder ()); }} สตริง [] dirarray = {}; if (dirs.size ()> 0) {dirarray = list2array (dirs); array.sort (Dirarray); if (! asc) {arrays.sort (Dirarray, collections.reverseroger ()); }} return concat2FileArray (filearray, dirarray); } / ** * concat2filearray merge array * * @param old1 * @param old2 * @return * / ไฟล์คงที่ส่วนตัว [] concat2filearray (สตริง [] old1, string [] old2) {ไฟล์ [] newarray = ไฟล์ใหม่ [old1.length + old2.length]; สำหรับ (int i = 0, n = old1.length; i <n; i ++) {newArray [i] = ไฟล์ใหม่ (old1 [i]); } สำหรับ (int i = 0, j = old1.length, n = (old1.length+old2.length); j <n; i ++, j ++) {newarray [j] = ไฟล์ใหม่ (old2 [i]); } ส่งคืน Newarray; } / ** * อ่านไฟล์ข้อความอ่าน * * @param filepath * @param filename * @param charset * @return * / public Stringbuilder public Stringbuilder อ่าน (String filepath, String filename, String Charset) {StringBuilder SB = สตริงใหม่ ลอง {String fullPath = combinePath (filePath, ชื่อไฟล์); ไฟล์ file = ไฟล์ใหม่ (FullPath); sb.append (filetools.tail (ไฟล์, false, 0, charset)); } catch (exception ex) {j2log (null, null, ex); } return sb; } / ** * อ่านไฟล์ข้อความอ่าน * * @param FullPath * @param Charset * @return * / String Stringbuilder สาธารณะอ่าน (String FullPath, String Charset) {StringBuilder SB = new StringBuilder (); ลอง {file file = ไฟล์ใหม่ (fullPath); sb.append (filetools.tail (ไฟล์, false, 0, charset)); } catch (exception ex) {j2log (null, null, ex); } return sb; } / ** * อ่านไฟล์ข้อความอ่านไฟล์ * * @param ไฟล์ * @param charset * @return * / public Stringbuilder read (ไฟล์ไฟล์, สตริง charset) {StringBuilder sb = new StringBuilder (); ลอง {sb.append (filetools.tail (ไฟล์, เท็จ, 0, charset)); } catch (exception ex) {j2log (null, null, ex); } return sb; } / ** * ค้นหาอ่านบรรทัดที่ระบุของไฟล์ข้อความ * * @param filepath * @param filename * @param line * @param charset * @return * / public Stringbuilder public Stringbuilder ค้นหา ลอง {String fullPath = combinePath (filePath, ชื่อไฟล์); ไฟล์ file = ไฟล์ใหม่ (FullPath); sb.append (filetools.tail (ไฟล์, true, line, charset)); } catch (exception ex) {j2log (null, null, ex); } return sb; } / ** * ค้นหาอ่านบรรทัดที่ระบุของไฟล์ข้อความ * * @param FullPath * @param line * @param charset * @return * / public stringbuilder find (สตริง FullPath, int line, charset สตริง) {stringbuilder sb = สตริงใหม่ ลอง {file file = ไฟล์ใหม่ (fullPath); sb.append (filetools.tail (ไฟล์, true, line, charset)); } catch (exception ex) {j2log (null, null, ex); } return sb; } / ** * ค้นหาอ่านบรรทัดที่ระบุของไฟล์ข้อความ * * @param ไฟล์ * @param line * @param charset * @return * / public Stringbuilder find find (ไฟล์ไฟล์, บรรทัด int, charset สตริง) {StringBuilder sb = new StringBuilder (); ลอง {sb.append (filetools.tail (ไฟล์, true, line, charset)); } catch (exception ex) {j2log (null, null, ex); } return sb; } / ** * หางอ่านไฟล์ข้อความ * * @param filepath * @param filename * @param charset * @param find * @param line * @return * / public Stringbuilder tail (สตริง filepath ลอง {String fullPath = combinePath (filePath, ชื่อไฟล์); ไฟล์ file = ไฟล์ใหม่ (FullPath); sb.append (filetools.tail (ไฟล์, ค้นหา, บรรทัด, charset)); } catch (exception ex) {j2log (null, null, ex); } return sb; } / ** * tail read text file * * @param fullPath * @param charset * @param find * @param line * @return * / public Stringbuilder tail (String FullPath, boolean find, int line, charset สตริง) {StringBuilder SB = สตริงใหม่ ลอง {file file = ไฟล์ใหม่ (fullPath); sb.append (filetools.tail (ไฟล์, ค้นหา, บรรทัด, charset)); } catch (exception ex) {j2log (null, null, ex); } return sb; } / ** * tail read text file * * ไฟล์ @param * @param charset * @param find * @param line * @return * / public Stringbuilder tail (ไฟล์ไฟล์, boolean find, int line, charset สตริง) {stringbuilder sb = สตริงใหม่ bufferedReader bufferreader = null; if (null == charset || "" .equals (charset)) {charset = "utf-8"; } ลอง {ถ้า (! file.exists () || file.isdirectory ()) {โยน filenotfoundexception ใหม่ (); } String fullPath = file.getPath (); bufferReader = ใหม่ bufferedReader (ใหม่ inputStreamReader (ใหม่ FileInputStream (FullPath), charset)); สตริงอุณหภูมิ; สำหรับ (int i = 0; (temp = bufferReader.readline ())! = null; i ++) {ถ้า (! ค้นหา || line == i) {sb.append (temp); }}} catch (Exception Ex) {j2log (null, null, ex); } ในที่สุด {ถ้า (null! = bufferReader) {ลอง {bufferReader.close (); } catch (ioexception ex) {j2log (null, null, ex); }}} ส่งคืน sb; } / ** * sed อ่านไฟล์ข้อความ * * @param filepath * @param filename * @param charset * @return * / รายการคงที่สาธารณะ <String> sed (สตริง filepath, ชื่อไฟล์สตริง, สตริง charset) {รายการ <string> รายการ = new ArrayList () ลอง {String fullPath = combinePath (filePath, ชื่อไฟล์); ไฟล์ file = ไฟล์ใหม่ (FullPath); list.addall (filetools.sed (ไฟล์, charset)); } catch (exception ex) {j2log (null, null, ex); } return list; } / ** * sed อ่านไฟล์ข้อความ * * @param fullPath * @param charset * @return * / รายการคงที่สาธารณะ <String> SED (String FullPath, String Charset) {list <string> list = new ArrayList (); ลอง {file file = ไฟล์ใหม่ (fullPath); list.addall (filetools.sed (ไฟล์, charset)); } catch (exception ex) {j2log (null, null, ex); } return list; } / ** * sed อ่านไฟล์ข้อความ * * ไฟล์ @param * @param charset * @return * / รายการคงที่สาธารณะ <String> SED (ไฟล์ไฟล์, สตริง charset) {list <string> list = new ArrayList (); bufferedReader bufferreader = null; if (null == charset || "" .equals (charset)) {charset = "utf-8"; } ลอง {ถ้า (! file.exists () || file.isdirectory ()) {โยน filenotfoundexception ใหม่ (); } String fullPath = file.getPath (); bufferReader = ใหม่ bufferedReader (ใหม่ inputStreamReader (ใหม่ FileInputStream (FullPath), charset)); สตริงอุณหภูมิ; สำหรับ (int i = 0; (temp = bufferReader.readline ())! = null; i ++) {list.add (temp); }} catch (exception ex) {j2log (null, null, ex); } ในที่สุด {ถ้า (null! = bufferReader) {ลอง {bufferReader.close (); } catch (ioexception ex) {j2log (null, null, ex); }}} รายการส่งคืน; } / ** * CAT อ่านไฟล์ข้อความ * * @param filePath * @param filename * @return * / public static byte [] cat (สตริง filepath, ชื่อไฟล์สตริง) {byte [] output = {}; ลอง {String fullPath = combinePath (filePath, ชื่อไฟล์); ไฟล์ file = ไฟล์ใหม่ (FullPath); output = filetools.cat (ไฟล์); } catch (exception ex) {j2log (null, null, ex); } return output; } / ** * CAT อ่านไฟล์ข้อความ * * @param FullPath * @return * / public Static byte [] cat (String fullPath) {byte [] output = {}; ลอง {file file = ไฟล์ใหม่ (fullPath); output = filetools.cat (ไฟล์); } catch (exception ex) {j2log (null, null, ex); } return output; } / ** * ไฟล์ข้อความอ่าน cat * * @param ไฟล์ * @return * / public static byte [] cat (ไฟล์ไฟล์) {inputstream ใน = null; ไบต์ [] เอาต์พุต = {}; ลอง {ถ้า (! file.exists () || file.isdirectory ()) {โยน filenotfoundexception ใหม่ (); } String fullPath = file.getPath (); ความยาวยาว = du (ไฟล์, เท็จ); ยาว _2m = 2097152; ไบต์ [] ไบต์ = ไบต์ใหม่ [(int) ความยาว]; ใน = new FileInputStream (FullPath); สำหรับ (int count = 0; count! = -1;) {ถ้า (ความยาว> 16 * _2m) {length = 4 * _2m; } อื่นถ้า (ความยาว> 8 * _2m) {length = 2 * _2m; } อื่นถ้า (ความยาว> 4 * _2m) {length = _2m; } อื่นถ้า (ความยาว> 2 * _2m) {length = _2m / 2; } อื่นถ้า (ความยาว> _2m) {length = _2m / 4; } else {length = 4096; } bytes = byte ใหม่ [(int) ความยาว]; count = in.read (ไบต์); เอาต์พุต = concatarray (ไบต์, เอาต์พุต); ความยาว = in.available (); }} catch (exception ex) {j2log (null, null, ex); } ในที่สุด {ถ้า (null! = in) {ลอง {in.close (); } catch (exception ex) {j2log (null, null, ex); }}} return output; } / ** * ผสานอาร์เรย์ * * @param old1 * @param old2 * @return * / ไบต์คงที่ส่วนตัว [] concatarray (byte [] old1, byte [] old2) {byte [] newarray = byte ใหม่ [old1.length + old2.length]; System.ArrayCopy (Old1, 0, Newarray, 0, Old1.Length); System.ArrayCopy (Old2, 0, NewArray, Old1.Length, Old2.Length); กลับนิวร์เรย์; } / ** * dd เขียนเนื้อหาเนื้อหาเนื้อหาเต็มรูปแบบ * * @param filepath * @param filename * @param เนื้อหา * @param isappend * / โมฆะคงที่สาธารณะ dd (สตริง filepath, สตริงชื่อไฟล์, ไบต์ [] เนื้อหา, บูลีน ไฟล์ file = ไฟล์ใหม่ (FullPath); filetools.dd (ไฟล์, เนื้อหา, iSappend); } catch (exception ex) {j2log (null, null, ex); }} / ** * dd เขียนไฟล์เนื้อหาเต็มเนื้อหาเนื้อหา * * @param fullpath * @param เนื้อหา * @param isappend * / โมฆะคงที่สาธารณะ dd (String fullpath, byte [] เนื้อหา, boolean isappend) {ลอง {ไฟล์ไฟล์ = ไฟล์ใหม่ (fullpath); filetools.dd (ไฟล์, เนื้อหา, iSappend); } catch (exception ex) {j2log (null, null, ex); }} / ** * dd เขียนไฟล์เนื้อหาเต็มเนื้อหาเนื้อหา * * @param ไฟล์ * @param เนื้อหา * @param isappend * / โมฆะคงที่สาธารณะ dd (ไฟล์ไฟล์, ไบต์ [] เนื้อหา, บูลีน iSappend) {fileOutputTream fileOutputTream = null; ลอง {ถ้า (! file.exists ()) {file.createnewfile (); } fileOutputStream = ใหม่ fileOutputStream (ไฟล์, iSappend); fileOutputStream.write (เนื้อหา); } catch (exception ex) {j2log (null, null, ex); } ในที่สุด {ลอง {ถ้า (null! = fileOutputStream) {fileOutputStream.close (); }} catch (ioexception ex) {j2log (null, null, ex); }}}} / ** * เขียนเนื้อหาการเขียนไฟล์ไฟล์ไปยังไฟล์ FullPath * * @param filepath * @param filename * @param เนื้อหา * / โมฆะคงที่สาธารณะเขียน (สตริง filepath, ชื่อไฟล์สตริง, เนื้อหาสตริง) {ลอง {String fullPath = CombinePath ไฟล์ file = ไฟล์ใหม่ (FullPath); filetools.write (ไฟล์, เนื้อหา, จริง); } catch (exception ex) {j2log (null, null, ex); }} / ** * เขียนเนื้อหาไฟล์เขียนไปยังไฟล์ FullPath * * @Param FullPath * @Param เนื้อหา * / โมฆะคงที่สาธารณะเขียน (String FullPath เนื้อหาสตริง) {ลอง {ไฟล์ไฟล์ = ไฟล์ใหม่ (เต็มรูปแบบ); filetools.write (ไฟล์, เนื้อหา, จริง); } catch (exception ex) {j2log (null, null, ex); }} / ** * เขียนเนื้อหาการเขียนไฟล์ไฟล์ไปยังไฟล์ FullPath * * @Param File * @param เนื้อหา * / โมฆะคงที่สาธารณะเขียน (ไฟล์ไฟล์, เนื้อหาสตริง) {ลอง {filetools.write (ไฟล์, เนื้อหา, จริง); } catch (exception ex) {j2log (null, null, ex); }} / ** * เขียนเขียนเนื้อหา (ผนวก) เนื้อหาไฟล์ไปยังไฟล์ FullPath * * @Param FilePath * @param filename * @param เนื้อหา * @param isappend * / โมฆะคงที่สาธารณะเขียน (สตริง filepath, สตริงชื่อไฟล์สตริง ไฟล์ file = ไฟล์ใหม่ (FullPath); filetools.write (ไฟล์, เนื้อหา, iSappend); } catch (exception ex) {j2log (null, null, ex); }} / ** * เขียนเขียนเนื้อหา (ผนวก) เนื้อหาไฟล์ไปยังไฟล์ FullPath * * @Param FullPath * เนื้อหา @param * @param isappend * / โมฆะคงที่สาธารณะเขียน (สตริงเต็ม pent, เนื้อหาสตริง, บูลีน isappend) {ลอง {ไฟล์ไฟล์ = ไฟล์ใหม่ filetools.write (ไฟล์, เนื้อหา, iSappend); } catch (exception ex) {j2log (null, null, ex); }} / ** * เขียนเนื้อหาการเขียน (ผนวก) เนื้อหาไฟล์ไปยังไฟล์ FullPath * * @param ไฟล์ * @param เนื้อหา * @param isappend * / โมฆะคงที่สาธารณะเขียน (ไฟล์ไฟล์, เนื้อหาสตริง ลอง {ถ้า (! file.exists ()) {file.createnewfile (); } fileWriter = ใหม่ fileWriter (file.getPath (), iSappend); filewriter.write (เนื้อหา); } catch (exception ex) {j2log (null, null, ex); } ในที่สุด {ถ้า (null! = fileWriter) {ลอง {fileWriter.close (); } catch (ioexception ex) {j2log (null, null, ex); }}}}} / ** * tail เพิ่มเนื้อหาไฟล์เนื้อหาไปยังตำแหน่งดัชนีของไฟล์ * * @param filepath * @param filename * @param เนื้อหา * @param index * / โมฆะคงที่สาธารณะ ไฟล์ file = ไฟล์ใหม่ (FullPath); filetools.tail (ไฟล์, เนื้อหา, ดัชนี); } catch (exception ex) {j2log (null, null, ex); }} / ** * tail เพิ่มเนื้อหาเนื้อหาไฟล์ไปยังตำแหน่งดัชนีของไฟล์ * * @param fullpath * @param เนื้อหา * @param index * / โมฆะคงที่สาธารณะ (สตริงเต็ม, เนื้อหาสตริง, ดัชนียาว) {ลอง {ไฟล์ไฟล์ = ไฟล์ใหม่ (เต็มรูปแบบ); filetools.tail (ไฟล์, เนื้อหา, ดัชนี); } catch (exception ex) {j2log (null, null, ex); }} / ** * tail เพิ่มเนื้อหาเนื้อหาไฟล์ไปยังตำแหน่งดัชนีของไฟล์ * * @param file * @param เนื้อหา * @param index * / โมฆะคงที่สาธารณะคงที่ (ไฟล์ไฟล์, เนื้อหาสตริง, ดัชนียาว) ลอง {ถ้า (! file.exists ()) {file.createnewfile (); } randomaccessFile = new randomActEncessFile (file.getPath (), "rw"); RandomaccessFile.seek (ดัชนี); RandomaccessFile.writebytes (เนื้อหา); } catch (exception ex) {j2log (null, null, ex); } ในที่สุด {ถ้า (null! = randomaccessFile) {ลอง {randomaccessFile.close (); } catch (exception ex) {j2log (null, null, ex); }}}}} / ** * mkdir สร้างไดเรกทอรี * * @param filepath * @param filename * @return * / ไฟล์คงที่สาธารณะ mkdir (สตริง filepath, ชื่อไฟล์สตริง) {ไฟล์ไฟล์ = null; ลอง {String fullPath = combinePath (filePath, ชื่อไฟล์); file = ไฟล์ใหม่ (FullPath); file = mkdir (ไฟล์); } catch (exception ex) {j2log (null, null, ex); } ส่งคืนไฟล์; } / ** * mkdir สร้างไดเรกทอรี * * @param FullPath * @return * / ไฟล์คงที่สาธารณะ mkdir (สตริงเต็ม path) {ไฟล์ไฟล์ = null; ลอง {file = ไฟล์ใหม่ (FullPath); file = mkdir (ไฟล์); } catch (exception ex) {j2log (null, null, ex); } ส่งคืนไฟล์; }/** * mkdir สร้างไดเรกทอรี * * @param ไฟล์ * @return */ไฟล์คงที่สาธารณะ mkdir (ไฟล์ไฟล์) {ลอง {ถ้า (! file.exists ()) {file.mkdir (); // สร้างถ้าโฟลเดอร์ไม่มีอยู่ } ส่งคืนไฟล์; } / ** * แตะสร้างไฟล์ * * @param filepath * @param filename * / public Static void touch (String filepath, String filename) {ลอง {String fullPath = combinePath (filePath, filename); ไฟล์ file = ไฟล์ใหม่ (FullPath); แตะ (ไฟล์); } catch (exception ex) {j2log (null, null, ex); }} / ** * แตะสร้างไฟล์ * * @param fullPath * / โมฆะคงที่สาธารณะสัมผัส (String FullPath) {ลอง {ไฟล์ไฟล์ = ไฟล์ใหม่ (เต็มรูปแบบ); แตะ (ไฟล์); } catch (exception ex) {j2log (null, null, ex); }}/** * แตะสร้างไฟล์ * * @param ไฟล์ */โมฆะคงที่สาธารณะสัมผัส (ไฟล์ไฟล์) {ลอง {ถ้า (! file.exists ()) {file.createnewfile (); // สร้างถ้าไฟล์ไม่มีอยู่}} catch }} / ** * rm ลบไฟล์ * * @param filepath * @param filename * / โมฆะคงที่สาธารณะ rm (สตริง filepath, ชื่อไฟล์สตริง) {ลอง {String fullPath = combinePath (filePath, filename); ไฟล์ file = ไฟล์ใหม่ (FullPath); rm (ไฟล์); } catch (exception ex) {j2log (null, null, ex); }} / ** * RM DELETE FILE * * @Param FullPath * / โมฆะคงที่สาธารณะ RM (String FullPath) {ลอง {file file = ไฟล์ใหม่ (FullPath); rm (ไฟล์); } catch (exception ex) {j2log (null, null, ex); }} / ** * ไฟล์ลบ rm * * @param ไฟล์ * / โมฆะคงที่สาธารณะ rm (ไฟล์ไฟล์) {ลอง {ถ้า (! file.exists ()) {โยน filenotfoundexception ใหม่ (); } if (file.isfile ()) {file.delete (); }} catch (exception ex) {j2log (null, null, ex); }} / ** * rmdir delete directory * * @param filepath * @param filename * @param loop * / โมฆะสาธารณะคงที่ rmdir (สตริง filepath, ชื่อไฟล์สตริง, ลูปบูลีน) ไฟล์ DIR = ไฟล์ใหม่ (FullPath); rmdir (dir, loop); } catch (exception ex) {j2log (null, null, ex); }} / ** * RMDIR DELETE DIRETE * * @Param FullPath * @Param Loop * / โมฆะสาธารณะคงที่ RMDIR (สตริง FullPath, Boolean Loop) {ลอง {ไฟล์ dir = ไฟล์ใหม่ (FullPath); rmdir (dir, loop); } catch (exception ex) {j2log (null, null, ex); }} / ** * rmdir delete directory * * @param dir * @param loop * / โมฆะสาธารณะคงที่ rmdir (ไฟล์ dir, boolean loop) {ลอง {ถ้า (! } if (dir.isdirectory ()) {ไฟล์ [] files = dir.listfiles (); ความยาว int = files.length; สำหรับ (int i = 0; i <length && loop; i ++) {ถ้า (ไฟล์ [i] .isdirectory ()) {rmdir (ไฟล์ [i], ลูป); } else {rm (ไฟล์ [i]); }} if (loop || length == 0) {dir.delete (); }}} catch (Exception Ex) {j2log (null, null, ex); }} / ** * du รับขนาดไฟล์จริง * * @param filepath * @param filename * @param loop * @return * / สาธารณะคงที่ long du (สตริง filepath, ชื่อไฟล์สตริง, บูลีนลูป) {ขนาดยาว = 0; ลอง {String fullPath = combinePath (filePath, ชื่อไฟล์); ไฟล์ file = ไฟล์ใหม่ (FullPath); size = du (ไฟล์, ลูป); } catch (exception ex) {j2log (null, null, ex); } ขนาดคืน; } / ** * du รับขนาดไฟล์จริง * * @param filepath * @param filename * @return * / public Static Long du (String filepath, String filename) {ขนาดยาว = 0; ลอง {String fullPath = combinePath (filePath, ชื่อไฟล์); ไฟล์ file = ไฟล์ใหม่ (FullPath); size = du (ไฟล์, เท็จ); } catch (exception ex) {j2log (null, null, ex); } ขนาดคืน; } / ** * du รับขนาดไฟล์จริง * * @param fullPath * @return * / สาธารณะคงที่ long du (String FullPath) {ขนาดยาว = 0; ลอง {file file = ไฟล์ใหม่ (fullPath); size = du (ไฟล์, เท็จ); } catch (exception ex) {j2log (null, null, ex); } ขนาดคืน; } / ** * du รับขนาดไฟล์จริง * * @param ไฟล์ * @return * / สาธารณะคงที่ long du (ไฟล์ไฟล์) {ขนาดยาว = 0; ลอง {size = du (ไฟล์, เท็จ); } catch (exception ex) {j2log (null, null, ex); } ขนาดคืน; } / ** * du รับขนาดไฟล์จริง * * @param fullPath * @param loop * @return * / สาธารณะคงที่ long du (String FullPath, boolean loop) {ขนาดยาว = 0; ลอง {file file = ไฟล์ใหม่ (fullPath); size = du (ไฟล์, ลูป); } catch (exception ex) {j2log (null, null, ex); } ขนาดคืน; } / ** * du รับขนาดไฟล์จริง * * @param ไฟล์ * @param loop * @return * / สาธารณะคงที่ long du (ไฟล์ไฟล์, boolean loop) {filechannel filechannel = null; ขนาดยาว = 0; ลอง {ถ้า (! file.exists ()) {โยน filenotfoundexception ใหม่ (); } if (file.isfile ()) {fileInputStream fis = ใหม่ fileInputStream (ไฟล์); fileChannel = fis.getChannel (); size = filechannel.size (); } อื่นถ้า (file.isdirectory ()) {file [] files = file.listfiles (); ความยาว int = files.length; สำหรับ (int i = 0; i <length && loop; i ++) {ถ้า (ไฟล์ [i] .isdirectory ()) {du (ไฟล์ [i], ลูป); } else {size += du (ไฟล์ [i], false); }}}} catch (Exception Ex) {j2log (null, null, ex); } ในที่สุด {ถ้า (null! = fileChannel) {ลอง {fileChannel.close (); } catch (exception ex) {j2log (null, null, ex); }}} ขนาดคืน; -ข้างต้นเป็นเรื่องเกี่ยวกับบทความนี้ฉันหวังว่ามันจะเป็นประโยชน์กับการเรียนรู้ของทุกคน