Um die Dateinamen einer großen Anzahl von amerikanischen bevorzugten Dramen zu ändern, schrieb ich kürzlich ein Widget mit Swing. Der Code ist der Dateiverarbeitungsteil. Der spezifische Inhalt ist wie folgt
Paket datei.steuern; Import Java.io.BufferedReader; Import Java.io.file; Import Java.io.fileinputStream; Import Java.io.FilenotFoundException; Import Java.io.fileOutputStream; Importieren von Java.io.IO.IO.IO.IO.IOISWRIER; java.io.randomaccessfile; import Java.nio.Bytebuffer; Import Java.nio.Channels.filechannel; Import Java.util.ArrayList; Import Java.util.Arrays; Import Java.util.Collections; Import Java.util.List; java.util.logging.logger; /** * * @Author S.WATSON */public Class FileTools {public Filetools () {}/** * FormatPath Escape -Dateiverzeichnis * * @param path * @return */public static String FormatPath (String Path) {return path.replaceAll ("////", "/"); } / ** * kombininePath -Dateipfad merge * * @param eins * @param zwei * @return * / private statische String combinePath (String eins, string zwei) {String dori = ""; EINS = NULL == EINS? "": Formatpath (EINS); Zwei = null == Zwei? "": Formatpath (Zwei); if (! Eins.endswith ("/") && zwei.indexof ("/")! = 0) {dori = eins + "/" + zwi; } else {dori = (EINS + Zwei) .ReplaceAll ("//", "/"); } return dori; } / ** * List2Array List Conversion Array * * @param list * @return * / private statische String [] list2Array (Liste) {String Array [] = (String []) list.toArray (new String [list.size ()]); Array zurückgeben; } / ** * CP -Kopie -Datei * * @param source * @param Ziel * @param Loop * @return * / public staticlist <Datei> cp (String -Quelle, String -Ziel, Boolean Loop) {list <Datei> list = new ArrayList (); try {file srcFile = new Datei (Quelle); Datei Desfile = neue Datei (Ziel); list.addall (CP (SrcFile, Desfile, Loop)); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückgabeliste; } / ** * CP -Kopie -Datei * * @param Source * @param Ziel * @param Schleife * @return * / public staticlist <Datei> CP (Dateiquelle, Dateiziel, Boolean Loop) {list <Datei> list = new ArrayList (); try {if (! source.exists () || source.isdirectory ()) {werfen neuer FilenotFoundException (); } list.add (CP (Quelle, Ziel)); if (Loop) {string [] subfile = source.list (); für (String subpath: subfile) {String src = comblePath (source.getPath (), subpath); // Der Original -Dateipfad der Subfile -Zeichenfolge Des = combinePath (Ziel.getPath (), Subpath); // Der Zielpfad der Subfile -Datei subfile = new Datei (SRC); if (subfile.isfile ()) {list.add (cp (src, des)); } else if (subfile.isdirectory () && loop) {list.addall (CP (SRC, DES, Schleife)); }}}}} catch (Ausnahme ex) {j2log (null, null, ex); } Rückgabeliste; } / ** * CP Einzeldatei -Datei * * @param source * @param Ziel * @return * / public statische Datei CP (String -Quelle, String -Ziel) {Datei Desfile = null; try {file srcFile = new Datei (Quelle); Desfile = neue Datei (Ziel); Desfile = CP (SrcFile, Desfile); } catch (Ausnahme ex) {J2Log (null, null, ex); } return Desfile; } / ** * CP -Datei mit einer Datei kopieren * * @param source * @param Ziel * @return * / public statische Datei CP (Dateiquelle, Dateiziel) {FileInputStream in = null; FileOutputStream out = null; Filechannel inc = null; Filechannel outc = null; try {if (! source.exists () || source.isdirectory ()) {werfen neuer FilenotFoundException (); } if (source.getPath (). Equals (destination.getPath ())) {return Source; } long AllBytes = du (Quelle, False); if (! destination.exists ()) {destination.createNewFile (); } in = new FileInputStream (source.getPath ()); out = new FileOutputStream (Ziel); Inc = in.getCannel (); outc = out.getChannel (); Bytebuffer bytebuffer = null; Langlänge = 2097152; // Grundgröße, Standard 2m lang _2M = 2097152; while (inc.position () <inc.size ()) {if (Allbytes> (64 * Länge)) {// Wenn die Dateigröße größer als 128 m ist, wird der Cache in 64 m Länge geändert = 32 * _2M; } else if (AllBytes> (32 * Länge)) {// Wenn die Dateigröße größer als 64 m ist, wird der Cache in 32 m Länge = 16 * _2M geändert; } else if (AllBytes> (16 * Länge)) {// Wenn die Dateigröße größer als 32 m ist, wird der Cache in 16m Länge = 8 * _2M geändert; } else if (Allbytes> (8 * Länge)) {// Wenn die Dateigröße größer als 16 m ist, wird der Cache in 8m Länge geändert = 4 * _2M; } else if (Allbytes> (4 * Länge)) {// Wenn die Dateigröße größer als 8 m ist, wird der Cache in 4m Länge = 2 * _2M geändert; } else if (AllBytes> (2 * Länge)) {// Wenn die Dateigröße größer als 4 m ist, wird der Cache in 2m Länge = _2M geändert; } else if (Allbytes> (Länge)) {// Wenn die Dateigröße größer als 2 m ist, wird der Cache in 1M Länge = _2M/2 geändert; } else if (AllBytes <Länge) {// Wenn die Datei kleiner als die Basisgröße ist, Ausgabe Länge direkt = Allbytes; } AllBytes = Inc.size () - Inc.Position (); bytebuffer = bytebuffer.allocatedirect ((int) Länge); Inc.Read (ByteBuffer); bytebuffer.flip (); outc.write (bytebuffer); outc.force (falsch); }} catch (Ausnahme ex) {j2log (null, null, ex); } endlich {try {if (null! = inc) {inc.close (); } if (null! = outc) {outc.close (); } if (null! = in) {in.close (); } if (null! = out) {out.close (); }} catch (Ausnahme ex) {j2log (null, null, ex); }} Rückgabeziel; } / ** * Datei umbenennen * * @param filepath * @param von * @param in * @return * / public static Datei umbenennen (String filepath, String von, String to) {Datei newfile = null; try {file oldFile = new Datei (CombainPath (Filepath, von)); newfile = new Datei (CombainPath (Filepath, TO)); umbenennen (newfile, oldFile); } catch (Ausnahme ex) {J2Log (null, null, ex); } neufile zurück; } / ** * Datei umbenennen * * @param zu * @param von * @return * / public statische Datei umbenennen (Datei von, Datei to) {try {string newpath = to.getPath (); String oldPath = von.getPath (); if (! OldPath.equals (newPath)) {if (! to.exists ()) {from.renameto (to); }}} catch (Ausnahme ex) {j2log (null, null, ex); } kehre zu; } / ** * MV MOVE -Datei * * @param Dateiname * @param Source * @param Ziel * @param Cover * / public static void MV (String -Dateiname, String Quelle, String -Ziel, boolean Cover) {try {if (! source.equals)) {Datei OldFile = new File = new Datei (Combainpath (Quelle, Filename); Datei newfile = new Datei (CombainPath (Ziel, Dateiname)); MV (OldFile, Newfile, Cover); }} catch (Ausnahme ex) {j2log (null, null, ex); }} / ** * MV MOVE -Datei * * @param source * @param Ziel * @param Cover * / public static void mv (String Quelle, Zeichenfolge Ziel, boolean Cover) {try {if (! source.equals (Ziel)) {Datei oldFile = new Datei (Quelle); Datei newfile = new Datei (Ziel); MV (OldFile, Newfile, Cover); }} catch (Ausnahme ex) {j2log (null, null, ex); }} / ** * MV MOVE -Datei * * @param source * @param Ziel * @param Cover * / public static void mv (Dateiquelle, Dateiziel, boolean Cover) {try {if (! source.exists ()) {throf New FilenotFoundException (); } StringBuilder FileName = new StringBuilder (source.getName ()); if (! cover && source.getPath (). Equals (destination.getPath ()) {if (fileName.indexof (".")> 0) {fileName.insert (fileName.lastIndexof ("."), "_copy"); } else {Dateiname.Append ("_ Copy"); } CP (Quelle, neue Datei (CombainPath (source.getParent (), Dateiname.toString ()))); } else {source.renameto (Ziel); }} catch (Ausnahme ex) {j2log (null, null, ex); }} / ** * Erweiterungen erhalten Sie Informationen zur Dateierweiterung * * @param filepath * @param fileName * @return * / private statische Zeichenfolge [] Erweiterungen (String -Filepath, String Dateiname) {String [] Erweiterung = {}; try {string fullPath = combinePath (filepath, fileName); Datei Datei = neue Datei (FullPath); Erweiterungen (Datei); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückkehrerweiterung; } / ** * Erweiterungen erhalten Dateierweiterungsinformationen * * @param fullPath * @return * / private statische String [] Erweiterungen (String FullPath) {String [] Erweiterung = {}; try {Datei file = new Datei (fullPath); Erweiterungen (Datei); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückkehrerweiterung; } / ** * Erweiterungen erhalten Sie Informationen zur Dateierweiterung * * @param -Datei * @return * / private statische String [] Erweiterungen (Dateidatei) {String [] Erweiterung = {}; try {if (file.isfile ()) {String Dateiname = file.getName (); if (fileName.lastIndexof (".")> = 0) {int lastIndex = fileName.lastIndexof ("."); Erweiterung [0] = String.ValueOf (lastIndex); // Index von "." Erweiterung [1] = Dateiname.substring (lastIndex + 1); // Erweiterungserweiterung [2] = Dateiname.substring (0, lastIndex); // Dateiname}}} catch (Ausnahme ex) {j2log (null, null, ex); } Rückkehrerweiterung; } / ** * ls durchqueren die Datei * * @param filepath * @param Schleife * @return * / public staticlist <Datei> ls (String filepath, boolean Loop) {list <Datei> list = new ArrayList (); try {Datei file = new Datei (filepath); list.addall (ls (Datei, Schleife)); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückgabeliste; } / ** * ls durchqueren die Datei * * @param Datei * @param Schleife * @return * / public staticlist <Datei> ls (Dateidatei, boolean Loop) {list <Datei> list = new ArrayList (); try {list.add (Datei); if (! file.isdirectory ()) {list.add (Datei); } else if (file.isdirectory ()) {file [] sublist = file.listfiles (); sublist = ffilessort (sublist, true); für (Datei subfile: sublist) {if (subfile.isdirectory () && loop) {list.addall (ls (subfile.getPath (), Loop)); } else {list.add (subfile); }}}} catch (Ausnahme ex) {j2log (null, null, ex); } Rückgabeliste; } / ** * ffilessortdatei sortieren (Standardaufstiegsauftrag) * * @param parentPath * @param sublist * @return * / private statische Datei [] ffilessort (Datei [] Infiles, boolean asc) {list <string> file = new ArrayList (); List <String> dirs = new ArrayList (); für (Datei -Subfile: Infiles) {if (subfile.isdirectory ()) {Dirs.add (subfile.getPath ()); } else if (subfile.isfile ()) {files.add (subfile.getPath ()); }} String [] filearray = {}; if (files.size ()> 0) {filearray = list2Array (Dateien); Arrays.sort (filearray); if (! asc) {arrays.sort (filearray, collections.reverseorder ()); }} String [] dirArray = {}; if (Dirs.SIZE ()> 0) {DirArray = list2Array (Dirs); Arrays.sort (DirArray); if (! asc) {arrays.sort (DirArray, Collections.Reverseorder ()); }} return concat2Filearray (filearray, dirArray); } / ** * concat2Filearray Merge -Dateiarray * * @param old1 * @param old2 * @return * / private statische Datei [] concat2Filearray (String [] Old1, String [] Old2) {Datei [] newArray = new Datei [old1.Length + old22.Length]; für (int i = 0, n = old1.length; i <n; i ++) {newArray [i] = neue Datei (old1 [i]); } für (int i = 0, j = old1.length, n = (old1.length+old2.Length); } return Newarray; } / ** * Lesen Sie die Textdatei * * * @param filepath * @param Dateiname * @param charset * @return * / public static StringBuilder Read (String FilePath, String Dateiname, String charSet) {StringBuilder sb = new StringBuilder (); try {string fullPath = combinePath (filepath, fileName); Datei Datei = neue Datei (FullPath); sb.Append (Filetools.tail (Datei, Falsch, 0, charset)); } catch (Ausnahme ex) {J2Log (null, null, ex); } return sb; } / ** * Lesen Sie die Textdatei * * * @param fullPath * @param charset * @return * / public static StringBuilder Read (String FullPath, String charSet) {StringBuilder sb = new StringBuilder (); try {Datei file = new Datei (fullPath); sb.Append (Filetools.tail (Datei, Falsch, 0, charset)); } catch (Ausnahme ex) {J2Log (null, null, ex); } return sb; } / ** * Lesen Sie die Textdatei * * * @param -Datei * @param charset * @return * / public static StringBuilder lesen (Dateidatei, String charSet) {StringBuilder sb = new StringBuilder (); try {sb.append (Filetools.tail (Datei, Falsch, 0, charset)); } catch (Ausnahme ex) {J2Log (null, null, ex); } return sb; } / ** * Suchen Sie die angegebene Zeile der Textdatei * * @param filepath * @param fileName * @param line * @param charset * @return * / public static StringBuilder find (String filePath, String Filename, int line, String charset) {stringBuilder sb = new StringBuilder (); try {string fullPath = combinePath (filepath, fileName); Datei Datei = neue Datei (FullPath); sb.Append (Filetools.tail (Datei, True, Line, Charset)); } catch (Ausnahme ex) {J2Log (null, null, ex); } return sb; } / ** * Lesen Sie die angegebene Zeile der Textdatei * * @param fullPath * @param line * @param charset * @return * / public static StringBuilder find (String fullPath, int line, string charset) {StringBuilder sb = new StringBuilder (); try {Datei file = new Datei (fullPath); sb.Append (Filetools.tail (Datei, True, Line, Charset)); } catch (Ausnahme ex) {J2Log (null, null, ex); } return sb; } / ** * Lesen Sie die angegebene Zeile der Textdatei * * @param Datei * @param Zeile * @param charSet * @return * / public static StringBuilder Find (Dateidatei, int Zeile, String charSet) {StringBuilder sb = new StringBuilder (); try {sb.append (Filetools.tail (Datei, True, Line, Charset)); } catch (Ausnahme ex) {J2Log (null, null, ex); } return sb; } / ** * Tail Lesen Sie die Textdatei * * @param filepath * @param Dateiname * @param charset * @param find * @param line * @return * / public static StringBuilder Tail (String filePath, String Filename, boolean find, int line, string charset) {StringBuilder sb = new StringBuilder (); try {string fullPath = combinePath (filepath, fileName); Datei Datei = neue Datei (FullPath); sb.Append (Filetools.tail (Datei, Find, Line, Charset)); } catch (Ausnahme ex) {J2Log (null, null, ex); } return sb; } / ** * Tail -Lesen -Textdatei * * @param fullPath * @param charset * @param find * @param line * @return * / public static StringBuilder Tail (String FullPath, boolean find, int line, string charset) {stringBuilder sb = new StringBuilder (); try {Datei file = new Datei (fullPath); sb.Append (Filetools.tail (Datei, Find, Line, Charset)); } catch (Ausnahme ex) {J2Log (null, null, ex); } return sb; } / ** * Tail -Lesen -Textdatei * * @param Datei * @param charset * @param find * @param line * @return * / public static StringBuilder Tail (Dateidatei, Boolean Find, Int Line, String charset) {StringBuilder sb = new StringBuilder (); BufferedReader bufferReader = null; if (null == charset || "" .Equals (charSet)) {charset = "utf-8"; } try {if (! file.exists () || file.isdirectory ()) {neue FilenotFoundException () werfen; } String fullPath = file.getPath (); bufferReader = new bufferedReader (neuer InputStreamReader (neuer FileInputStream (FullPath), charSet)); String -Temperatur; für (int i = 0; (temp = bufferreader.readline ())! }}} catch (Ausnahme ex) {j2log (null, null, ex); } endlich {if (null! } catch (ioException ex) {j2log (null, null, ex); }}} return sb; } / ** * sed Textdatei lesen * * @param filepath * @param Dateiname * @param charset * @return * / public staticlist <string> sed (String filepath, String -Dateiname, String charSet) {list <string> list = new ArrayList (); try {string fullPath = combinePath (filepath, fileName); Datei Datei = neue Datei (FullPath); list.addall (FileTools.sed (Datei, charSet)); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückgabeliste; } / ** * sed Textdatei lesen * * @param fullPath * @param charset * @return * / public static list <string> sed (String FullPath, String charSet) {list <string> list = new ArrayList (); try {Datei file = new Datei (fullPath); list.addall (FileTools.sed (Datei, charSet)); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückgabeliste; } / ** * sed Textdatei lesen * * @param Datei * @param charset * @return * / public staticlist <string> sed (Dateidatei, String charSet) {list <string> list = new ArrayList (); BufferedReader bufferReader = null; if (null == charset || "" .Equals (charSet)) {charset = "utf-8"; } try {if (! file.exists () || file.isdirectory ()) {neue FilenotFoundException () werfen; } String fullPath = file.getPath (); bufferReader = new bufferedReader (neuer InputStreamReader (neuer FileInputStream (FullPath), charSet)); String -Temperatur; für (int i = 0; (temp = bufferreader.readline ())! = null; i ++) {list.add (temp); }} catch (Ausnahme ex) {j2log (null, null, ex); } endlich {if (null! } catch (ioException ex) {j2log (null, null, ex); }}} Rückgabeliste; } / ** * CAT -Lesen Sie die Textdatei * * @param filepath * @param Dateiname * @return * / public static byte [] cat (String -Filepath, String -Dateiname) {byte [] output = {}; try {string fullPath = combinePath (filepath, fileName); Datei Datei = neue Datei (FullPath); output = Filetools.cat (Datei); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückgabeausgabe; } / ** * CAT -Lesen Sie die Textdatei * * @param fullPath * @return * / public static byte [] cat (String fullPath) {byte [] output = {}; try {Datei file = new Datei (fullPath); output = Filetools.cat (Datei); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückgabeausgabe; } / ** * CAT -Lesen Sie die Textdatei * * @param -Datei * @return * / public static byte [] cat (Dateidatei) {inputStream in = null; byte [] output = {}; try {if (! file.exists () || file.isdirectory ()) {werfen neuer FilenotFoundException (); } String fullPath = file.getPath (); Langlänge = du (Datei, Falsch); lang _2m = 2097152; byte [] bytes = new Byte [(int) Länge]; in = new FileInputStream (FullPath); für (int count = 0; count! = -1;) {if (länge> 16 * _2m) {Länge = 4 * _2M; } else if (Länge> 8 * _2M) {Länge = 2 * _2M; } else if (Länge> 4 * _2M) {Länge = _2M; } else if (Länge> 2 * _2M) {Länge = _2M / 2; } else if (Länge> _2M) {Länge = _2M / 4; } else {Länge = 4096; } bytes = new Byte [(int) Länge]; count = in.read (bytes); output = concatArray (Bytes, Ausgabe); Länge = in.Available (); }} catch (Ausnahme ex) {j2log (null, null, ex); } endlich {if (null! = in) {try {in.close (); } catch (Ausnahme ex) {J2Log (null, null, ex); }}} Rückgabeausgabe; } / ** * Array zusammenführen * * @param old1 * @param old2 * @return * / private static byte [] concatArray (byte [] old1, byte [] Old2) {byte [] newarray = new1.1.Length + old2.Length]; System.ArrayCopy (Old1, 0, Newarray, 0, Old1.Length); System.ArrayCopy (Old2, 0, Newarray, Old1.Length, Old2.Length); Rückkehr Newarray; } / ** * DD -Datei FullPath -Inhalt inhaltlich * * @param filepath * @param fileName * @param content * @param isAppend * / public static void dd (String filePath, String -Dateiname, Byte [] Inhalt, boolean isAppend) {try {string fullpath = combinepath (filepath, fileName); Datei Datei = neue Datei (FullPath); Filetools.dd (Datei, Inhalt, ISAppend); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * DD -Datei fullPath -Inhalt inhaltlich * * @param fullPath * @param content * @param isAppend * / public static void dd (String FullPath, Byte [] Inhalt, boolean isAppend) {try {file file = new Datei (fullPath); Filetools.dd (Datei, Inhalt, ISAppend); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * DD -Datei FullPath -Inhalt inhaltlich * * @param Datei * @param Inhalt * @param isAppend * / public static void dd (Dateidatei, Byte [] Inhalt, boolean isAppend) {FileOutputStream -DateiputStream = null; try {if (! file.exists ()) {file.createNewFile (); } FileOutputStream = new FileOutputStream (Datei, ISAppend); FileOutputStream.write (Inhalt); } catch (Ausnahme ex) {J2Log (null, null, ex); } endlich {try {if (null! }} catch (ioException ex) {j2log (null, null, ex); }}}} / ** * Schreibdateiinhalt schreiben, um fullPath zu dünen * * @param filepath * @param Dateiname * @param Inhalt * / public static void write (String filepath, String FileName, String -Inhalt) {try {String fullPath = combinePath (Filepath, Filename); Datei Datei = neue Datei (FullPath); Filetools.Write (Datei, Inhalt, true); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * Schreibdatei -Inhalt schreiben, um fullPath zu Datei * * * @param fullPath * @param Inhalt * / public static void write (String FullPath, String -Inhalt) {try {file Datei = new Datei (fullPath); Filetools.Write (Datei, Inhalt, true); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * Schreibdatei -Inhalt schreiben, um FullPath zu Datei * * @param -Datei * @param -Inhalt * / public static void write (Dateidatei, String -Inhalt) {try {Filetools.Write (Datei, Inhalt, True); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * Schreiben Sie einen Dateiinhalt schreiben (append) in Datei fullPath * * @param filepath * @param fileName * @param content * @param isAppend * / public static void write (String filePath, String -FileName, String -Inhalt, boolean isAppend) {try {string fullpath = combinePheSe (filepath, filepath); Datei Datei = neue Datei (FullPath); Filetools.Write (Datei, Inhalt, ISAppend); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * Schreiben Sie einen Dateiinhalt schreiben (anhängen) in Datei in Datei fullPath * * @param fullPath * @param content * @param isAppend * / public static void write (String FullPath, String -Inhalt, boolean isAppend) {try {file file = new Datei (FullPath); Filetools.Write (Datei, Inhalt, ISAppend); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * Schreiben Sie einen Dateiinhalt schreiben (anhängen) in Datei in Datei fullPath * * @param Datei * @param -Inhalt * @param isAppend * / public static void write (Dateidatei, String -Inhalt, boolean isAppend) {FileWriter FileWriter = null; try {if (! file.exists ()) {file.createNewFile (); } FileWriter = new FileWriter (file.getPath (), isAppend); FileWriter.Write (Inhalt); } catch (Ausnahme ex) {J2Log (null, null, ex); } endlich {if (null! } catch (ioException ex) {j2log (null, null, ex); }}}}} / ** * Schwanz Fügen Sie den Inhaltsinhalt des Datei in den Indexspeicherort der Datei hinzu * * @param filepath * @param Dateiname * @param -Inhalt * @param index * / public static void Tail (String Filepath, String FileName, String -Inhalt, langer Index) {Try {String Fullpath = combinePheSphen (Filepath); Datei Datei = neue Datei (FullPath); Filetools.tail (Datei, Inhalt, Index); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * Schwanz fügen Sie Dateiinhalt in den Indexspeicherort der Datei hinzu * * @param fullPath * @param -Inhalt * @param index * / public static void Tail (String FullPath, String -Inhalt, langer Index) {try {file file = new Datei (fullPath); Filetools.tail (Datei, Inhalt, Index); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * Schwanz fügen Sie den Inhalt des Dateiinhalts zum Indexspeicherort der Datei * * @param Datei * @param -Inhalt * @param index * / public static void Tail (Dateidatei, String -Inhalt, Long Index) {RandomAccessFile RandomAccessFile = null hinzu; try {if (! file.exists ()) {file.createNewFile (); } randomAccessfile = new randomAccessfile (file.getPath (), "rw"); randomAccessfile.seek (index); randomAccessfile.writebytes (Inhalt); } catch (Ausnahme ex) {J2Log (null, null, ex); } endlich {if (null! = randomAccessfile) {try {randomAccessfile.close (); } catch (Ausnahme ex) {J2Log (null, null, ex); }}}}} / ** * mkdir erstellen Verzeichnis * * @param filepath * @param fileName * @return * / public statische Datei mkdir (String filePath, String -Dateiname) {Datei Datei = null; try {string fullPath = combinePath (filepath, fileName); Datei = neue Datei (FullPath); Datei = mkdir (Datei); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückgabedatei; } / ** * mkdir erstellen Verzeichnis * * @param fullPath * @return * / public statische Datei mkdir (String FullPath) {file file = null; try {file = new Datei (fullPath); Datei = mkdir (Datei); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückgabedatei; }/** * mkdir erstellen Verzeichnis * * @param Datei * @return */public statische Datei mkdir (Dateidatei) {try {if (! file.exists ()) {file.mkdir (); // erstellen, wenn der Ordner nicht existiert}} catch (Exception ex) {j2log (null, null, null, null, null, null, null, null, null, null, null, ex); } Rückgabedatei; } / ** * touch create Datei * * @param filepath * @param fileName * / public static void touch (String filepath, String -Dateiname) {try {string fullPath = combinePath (filepath, fileName); Datei Datei = neue Datei (FullPath); Berührung (Datei); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * touch create Datei * * @param fullPath * / public static void touch (string fullPath) {try {file Datei = new Datei (fullPath); Berührung (Datei); } catch (Ausnahme ex) {J2Log (null, null, ex); }}/** * tourt creat create -Datei * * @param Datei */public static void touch (Dateidatei) {try {if (! file.exists ()) {file.createNewFile (); // erstellen, wenn die Datei nicht existiert}} catch (Ausnahme) {j2log (null, null, ex, ex); }} / ** * rm die Datei löschen * * @param filepath * @param fileName * / public static void rm (String filepath, String Dateiname) {try {String fullPath = combinePath (Filepath, FileName); Datei Datei = neue Datei (FullPath); RM (Datei); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * rm lösche Datei * * @param fullPath * / public static void rm (String fullPath) {try {file file = new File (fullPath); RM (Datei); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * rm lösche Datei * * @param Datei * / public static void rm (Dateidatei) {try {if (! file.exists ()) {thrower New FilenotFoundException (); } if (file.isfile ()) {file.delete (); }} catch (Ausnahme ex) {j2log (null, null, ex); }} / ** * rmdir löschen verzeichnis * * @param filepath * @param fileName * @param Schleife * / public static void rmdir (String filepath, String fileName, boolean Loop) {try {String fullPath = comblePath (FilePath, FileName); Datei Dir = New Datei (FullPath); rmdir (Dir, Loop); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * RMDIR DELETE -Verzeichnis * * @param fullPath * @param Schleife * / public static void rmdir (String FullPath, boolean Loop) {try {file dire = new Datei (fullPath); rmdir (Dir, Loop); } catch (Ausnahme ex) {J2Log (null, null, ex); }} / ** * rmdir delete verzeichnis * * @param dir * @param loop * / public static void rmdir (Datei dir, boolean Loop) {try {if (! dir.exists ()) {throf New FilenotFoundException (); } if (dire.isdirectory ()) {file [] file = dir.listfiles (); int länge = Dateien.length; für (int i = 0; i <lenge && loop; i ++) {if (Dateien [i] .isDirectory ()) {rmdir (Dateien [i], Schleife); } else {rm (Dateien [i]); }} if (Loop || Länge == 0) {Dir.Delete (); }}} catch (Ausnahme ex) {j2log (null, null, ex); }} / ** * DU die tatsächliche Dateigröße erhalten * * @param filepath * @param fileName * @param Schleife * @return * / public static long du (String -Filepath, String Dateiname, Boolean Loop) {Long Size = 0; try {string fullPath = combinePath (filepath, fileName); Datei Datei = neue Datei (FullPath); size = du (Datei, Schleife); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückkehrgröße; } / ** * DU Die tatsächliche Dateigröße erhalten * * @param filepath * @param Dateiname * @return * / public static long du (String filepath, String -Dateiname) {Long Size = 0; try {string fullPath = combinePath (filepath, fileName); Datei Datei = neue Datei (FullPath); size = du (Datei, false); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückkehrgröße; } / ** * DU den tatsächlichen Dateigröße erhalten * * @param fullPath * @return * / public static long du (String FullPath) {Long Size = 0; try {Datei file = new Datei (fullPath); size = du (Datei, false); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückkehrgröße; } / ** * du die tatsächliche Dateigröße erhalten * * @param Datei * @return * / public static long du (Dateidatei) {Long Size = 0; try {size = du (Datei, false); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückkehrgröße; } / ** * DU den tatsächlichen Dateigröße erhalten * * @param fullPath * @param Schleife * @return * / public static long du (String FullPath, boolean Loop) {Long Size = 0; try {Datei file = new Datei (fullPath); size = du (Datei, Schleife); } catch (Ausnahme ex) {J2Log (null, null, ex); } Rückkehrgröße; } / ** * du die tatsächliche Dateigröße erhalten * * @param Datei * @param Schleife * @return * / public static long du (Dateidatei, boolesche Schleife) {filechannel filechannel = null; lange Größe = 0; try {if (! file.exists ()) {throw New FilenotFoundException (); } if (file.isfile ()) {FileInputStream fis = new FileInputStream (Datei); filechannel = fis.getchannel (); size = filechannel.size (); } else if (file.isdirectory ()) {file [] file = file.listfiles (); int länge = Dateien.length; für (int i = 0; i <Länge && Loop; i ++) {if (Dateien [i] .isDirectory ()) {du (Dateien [i], Schleife); } else {size += du (Dateien [i], false); }}}} catch (Ausnahme ex) {j2log (null, null, ex); } endlich {if (null! = filechannel) {try {filechannel.close (); } catch (Ausnahme ex) {J2Log (null, null, ex); }}} Rückgabegröße; }}Das Obige dreht sich alles um diesen Artikel, ich hoffe, es wird für das Lernen aller hilfreich sein.