Récemment, afin de modifier les noms de fichiers d'un grand nombre de drames américains préférés, j'ai écrit un widget utilisant Swing. Le code est la pièce de traitement de fichiers. Le contenu spécifique est le suivant
Package DateI.Steuern; Importer java.io.bufferedReader; import java.io.file; import java.io.fileInputStream; import java.io.filenotfoundException; import java.io.fileoutputStream; import java.io.filewriter; import; java.io.randomaccessfile; import java.nio.bytebuffer; import java.nio.channels.filechannel; import java.util.arraylist; import java.util.array java.util.logging.logger; / ** * * @author s.watson * / public class filetools {public filetools () {} / ** * FormatPath Escape File Directory * * @param path * @return * / public static String FormatPath (String Path); } / ** * combinePath File Path Merge * * @param eins * @param zwei * @return * / private static 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 + "/" + zwei; } else {dori = (eins + zwei) .replaceAll ("//", "/"); } return dori; } / ** * list2Array List Conversion Array * * @param list * @return * / private static String [] list2Array (list list) {String Array [] = (String []) list.toArray (new String [list.size ()]); Tableau de retour; } / ** * CP Copy File * * @param source * @param Destination * @param Loop * @return * / public static list <File> cp (String Source, String Destination, Boolean Loop) {list <File> list = new ArrayList (); essayez {fichier srcfile = nouveau fichier (source); Fichier DesFile = nouveau fichier (destination); list.addall (cp (srcfile, Desfile, Loop)); } catch (exception ex) {j2Log (null, null, ex); } Retour List; } / ** * CP Copy File * * @param source * @param Destination * @param Loop * @return * / public static list <File> cp (File Source, File Destination, Boolean Loop) {list <FichE> list = new ArrayList (); essayez {if (! Source.exists () || source.isdirectory ()) {throw new filenotFoundException (); } list.add (cp (source, destination)); if (loop) {String [] subfile = source.list (); for (String subpath: subfile) {String src = combinagePath (source.getPath (), subpath); // le chemin de fichier d'origine de la chaîne de sous-fichier des = combinePath (destination.getPath (), subpath); // le chemin cible du fichier subfile subfile = nouveau fichier (src); if (subfile.isfile ()) {list.add (cp (src, des)); } else if (subfile.isdirectory () && loop) {list.addall (cp (src, des, loop)); }}}}} catch (exception ex) {j2Log (null, null, ex); } Retour List; } / ** * CP Fichier de copie de fichier unique * * @param source * @param destination * @return * / public static File CP (chaîne source, string Destination) {file Desfile = null; essayez {fichier srcfile = nouveau fichier (source); DesFile = nouveau fichier (destination); desfile = cp (srcfile, desfile); } catch (exception ex) {j2Log (null, null, ex); } return desfile; } / ** * CP Fichier de copie de fichier unique * * @param source * @param Destination * @return * / public static File CP (fichier source, fichier destination) {fileInputStream in = null; FileoutputStream out = null; FileChannel Inc = null; FileChannel OUTC = NULL; essayez {if (! Source.exists () || source.isdirectory ()) {throw new filenotFoundException (); } if (source.getPath (). equals (Destination.getPath ())) {return source; } long allbytes = du (source, false); if (! Destination.exists ()) {Destination.CreateEwFile (); } in = new FileInputStream (source.getPath ()); out = new FileOutputStream (destination); inc = in.getChannel (); outc = out.getChannel (); Bytebuffer bytebuffer = null; Longueur longue = 2097152; // Taille de base, par défaut 2 m de long _2M = 2097152; while (incc.position () <inc.size ()) {if (allbytes> (64 * longueur)) {// Si la taille du fichier est supérieure à 128m, le cache est modifié en longueur de 64 m = 32 * _2m; } else if (allbytes> (32 * longueur)) {// Si la taille du fichier est supérieure à 64m, le cache est modifié à 32 m de longueur = 16 * _2m; } else if (allbytes> (16 * longueur)) {// Si la taille du fichier est supérieure à 32 m, le cache est modifié à 16 m de longueur = 8 * _2m; } else if (allbytes> (8 * longueur)) {// Si la taille du fichier est supérieure à 16m, le cache est modifié à 8 m de longueur = 4 * _2m; } else if (allbytes> (4 * longueur)) {// Si la taille du fichier est supérieure à 8m, le cache est modifié en longueur de 4m = 2 * _2m; } else if (allbytes> (2 * longueur)) {// Si la taille du fichier est supérieure à 4m, le cache est modifié en longueur de 2m = _2m; } else if (allbytes> (longueur)) {// Si la taille du fichier est supérieure à 2m, le cache est modifié en longueur de 1m = _2m / 2; } else if (allbytes <longueur) {// Si le fichier est plus petit que la taille de base, longueur de sortie directement = allbytes; } allbytes = inc.size () - inc.OSITION (); bytebuffer = bytebuffer.AllocateIrct ((int) longueur); Inc.Read (ByteBuffer); bytebuffer.flip (); outc.write (bytebuffer); Outc.force (false); }} catch (exception ex) {j2Log (null, null, ex); } enfin {try {if (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); }} return destination; } / ** * Renommer le fichier Rename * * @param filepath * @param de * @param to * @return * / public static File Rename (String filepath, String from, String to) {file newfile = null; try {file oldfile = new File (combainPath (filepath, from)); newFile = new File (combainPath (filepath, to)); Renommer (NewFile, Oldfile); } catch (exception ex) {j2Log (null, null, ex); } return newfile; } / ** * Renommer le fichier Rename * * @param vers * @param from * @return * / public static file renom (file from, file to) {try {String newpath = to.getPath (); String oldpath = from.getPath (); if (! oldpath.equals (newpath)) {if (! to.exists ()) {from.renameto (to); }}} catch (exception ex) {j2Log (null, null, ex); } retour à; } / ** * MV Mov Fichier * * @param FileName * @param source * @param destination * @param couverture * / public static void mv (String FileName, chaîne source, string destination, booléen couverture) {try {if (! Source.equals (destination)) {file oldfile = nouveau file (combainpath (source, filename)); File newFile = new Fichier (combainPath (destination, nom de fichier)); MV (Oldfile, Newfile, couverture); }} catch (exception ex) {j2Log (null, null, ex); }} / ** * MV Move Fichier * * @param Source * @param Destination * @param Cover * / public static void mv (String Source, String Destination, booléan couverture) {try {if (! Source.equals (Destination)) {file oldfile = new File (source); File newFile = new File (destination); MV (Oldfile, Newfile, couverture); }} catch (exception ex) {j2Log (null, null, ex); }} / ** * Mv Move File * * @param source * @param destination * @param couverture * / public static void mv (fichier source, fichier destin, booléen couverture) {try {if (! source.exists ()) {lance 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 {filename.append ("_ copie"); } cp (source, nouveau fichier (combainPath (source.getParent (), filename.toString ()))); } else {source.renameto (destination); }} catch (exception ex) {j2Log (null, null, ex); }} / ** * Extensions Obtenir des informations d'extension de fichier * * @param filepath * @param filename * @return * / private static String [] extensions (String filepath, string filename) {String [] extension = {}; try {String fullPath = combinePath (filepath, filename); Fichier fichier = nouveau fichier (fullpath); extensions (fichier); } catch (exception ex) {j2Log (null, null, ex); } extension de retour; } / ** * Extensions Get File Extension Informations * * @param fullPath * @return * / private static String [] extensions (String fullPath) {String [] extension = {}; try {file file = new File (fullPath); extensions (fichier); } catch (exception ex) {j2Log (null, null, ex); } extension de retour; } / ** * Extensions Obtenir des informations d'extension de fichier * * @param fichier * @return * / private static String [] extensions (fichier file) {String [] extension = {}; try {if (file.isfile ()) {string filename = file.getName (); if (filename.LastIndexof (".")> = 0) {int lastIndex = filename.LastIndexof ("."); Extension [0] = String.ValueOf (LastIndex); // Index de "." Extension [1] = filename.substring (LastIndex + 1); // Extension Extension [2] = filename.substring (0, LastIndex); // Nom de fichier}} Catch (Exception ex) {J2Log (null, null, ex); } extension de retour; } / ** * ls Traversage du fichier * * @param filepath * @param boucle * @return * / public static list <File> ls (String filepath, boolean loop) {list <Fich> list = new ArrayList (); try {file file = new File (filepath); list.addall (ls (fichier, boucle)); } catch (exception ex) {j2Log (null, null, ex); } Retour List; } / ** * ls Traversage du fichier * * @param fichier * @param Loop * @return * / public static list <File> ls (fichier file, booléan Loop) {list <Fich> list = new ArrayList (); essayez {list.add (fichier); if (! file.isdirectory ()) {list.add (fichier); } else if (file.isdirectory ()) {file [] sublist = file.listFiles (); sublist = filessort (sublist, true); for (file subfile: sublist) {if (subfile.isdirectory () && loop) {list.addall (ls (subfile.getPath (), loop)); } else {list.add (subfile); }}}} catch (exception ex) {j2Log (null, null, ex); } Retour List; } / ** * FILESSORT FILE SORT (par défaut Ordre ascendant) * * @param parentPath * @param sublist * @return * / Fichier statique privé [] filessort (file [] infiles, booléen as) {list <string> files = new ArrayList (); List <string> dirs = new ArrayList (); for (file 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 (fichiers); Arrays.Sort (filearray); if (! asc) {arrays.sort (filearray, collection.reverseorder ()); }} String [] dirArray = {}; if (dirs.size ()> 0) {dirArray = list2Array (dirs); Arrays.sort (dirarray); if (! asc) {arrays.sort (dirArray, Collection.Reverseorder ()); }} return concat2FileArray (filearray, dirArray); } / ** * Concat2Filearray Merge File Array * * @param old1 * @param old2 * @return * / private static Fichier [] Concat2Filearray (String [] old1, String [] old2) {file [] newArray = new File [old1.length + old2.length]; for (int i = 0, n = old1.length; i <n; i ++) {newArray [i] = nouveau fichier (old1 [i]); } pour (int i = 0, j = old1.length, n = (old1.length + old2.length); j <n; i ++, j ++) {newArray [j] = nouveau fichier (old2 [i]); } return newArray; } / ** * Lire le fichier texte en lecture * * @param filepath * @param filename * @param charset * @return * / public static stringBuilder read (string filepath, string filename, string chartset) {stringBuilder sb = new StringBuilder (); try {String fullPath = combinePath (filepath, filename); Fichier fichier = nouveau fichier (fullpath); sb.append (filetools.tail (fichier, false, 0, charse)); } catch (exception ex) {j2Log (null, null, ex); } return sb; } / ** * Lire le fichier texte en lecture * * @param fullpath * @param charset * @return * / public static stringBuilder read (String fullPath, String charSet) {stringBuilder sb = new StringBuilder (); try {file file = new File (fullPath); sb.append (filetools.tail (fichier, false, 0, charse)); } catch (exception ex) {j2Log (null, null, ex); } return sb; } / ** * Lire le fichier texte en lecture * * @param fichier * @param charset * @return * / public static stringBuilder read (fichier file, string charset) {stringBuilder sb = new StringBuilder (); essayez {sb.append (filetools.tail (fichier, false, 0, charse)); } catch (exception ex) {j2Log (null, null, ex); } return sb; } / ** * Recherchez la ligne spécifiée du fichier texte * * @param filepath * @param filename * @param line * @param charset * @return * / public static stringBuilder find (String filepath, string fileNa, int, string charset) {stringbuilder sb = new StringBuilder (); try {String fullPath = combinePath (filepath, filename); Fichier fichier = nouveau fichier (fullpath); SB.APPEND (filetools.tail (fichier, true, ligne, charse)); } catch (exception ex) {j2Log (null, null, ex); } return sb; } / ** * Recherchez la ligne spécifiée du fichier texte * * @param fullPath * @param line * @param charset * @return * / public stating stringBuilder find (String fullPath, int line, string charset) {StringBuilder sb = new StringBuilder (); try {file file = new File (fullPath); SB.APPEND (filetools.tail (fichier, true, ligne, charse)); } catch (exception ex) {j2Log (null, null, ex); } return sb; } / ** * Rechercher la ligne spécifiée du fichier texte * * @param fichier * @param line * @param charset * @return * / public static stringBuilder find (fichier file, int line, string charset) {stringBuilder sb = new StringBuilder (); essayez {sb.append (filetools.tail (fichier, true, ligne, charse)); } catch (exception ex) {j2Log (null, null, ex); } return sb; } / ** * queue Lire le fichier texte * * @param filepath * @param filename * @param charset * @param find * @param line * @return * / public stating stringBuilder tail (string filepath, string filename, boolean find, int line, string charset) {StringBuilder sb = new StringBuilder (); try {String fullPath = combinePath (filepath, filename); Fichier fichier = nouveau fichier (fullpath); SB.APPEND (filetools.tail (fichier, trouver, ligne, charse)); } catch (exception ex) {j2Log (null, null, ex); } return sb; } / ** * Tail Read Fichier Text * * @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 {file file = new File (fullPath); SB.APPEND (filetools.tail (fichier, trouver, ligne, charse)); } catch (exception ex) {j2Log (null, null, ex); } return sb; } / ** * Fichier texte de lecture de queue * * @param fichier * @param charset * @param find * @param line * @return * / public static stringBuilder tail (fichier de fichier, booléen find, int line, string charset) {StringBuilder sb = new StringBuilder (); BufferedReader BufferReader = null; if (null == charset || "" .equals (charset)) {charset = "utf-8"; } essayez {if (! file.exists () || file.isDirectory ()) {lancer un nouveau filenotfoundException (); } String fullpath = file.getPath (); BufferReader = new BufferedReader (new inputStreamReader (new FileInputStream (fullPath), charset)); Chaîne temporaire; for (int i = 0; (temp = bufferReader.readline ())! = null; i ++) {if (! find || line == i) {sb.append (temp); }}} catch (exception ex) {j2Log (null, null, ex); } enfin {if (null! = bufferReader) {try {bufferReader.close (); } catch (ioException ex) {j2Log (null, null, ex); }}} return sb; } / ** * SED Read Fichier texte * * @param filepath * @param filename * @param charset * @return * / public static list <string> sed (string filepath, string fileName, String charset) {list <string> list = new ArrayList (); try {String fullPath = combinePath (filepath, filename); Fichier fichier = nouveau fichier (fullpath); list.addall (filetools.sed (fichier, charse)); } catch (exception ex) {j2Log (null, null, ex); } Retour List; } / ** * SED LIRE Fichier texte * * @param FullPath * @param charset * @return * / public static list <string> sed (String fullPath, String charSet) {list <string> list = new ArrayList (); try {file file = new File (fullPath); list.addall (filetools.sed (fichier, charse)); } catch (exception ex) {j2Log (null, null, ex); } Retour List; } / ** * SED LIRE Fichier texte * * @param fichier * @param charset * @return * / public static list <string> sed (fichier file, String charSet) {list <string> list = new ArrayList (); BufferedReader BufferReader = null; if (null == charset || "" .equals (charset)) {charset = "utf-8"; } essayez {if (! file.exists () || file.isDirectory ()) {lancer un nouveau filenotfoundException (); } String fullpath = file.getPath (); BufferReader = new BufferedReader (new inputStreamReader (new FileInputStream (fullPath), charset)); Chaîne temporaire; for (int i = 0; (temp = bufferReader.readline ())! = null; i ++) {list.add (temp); }} catch (exception ex) {j2Log (null, null, ex); } enfin {if (null! = bufferReader) {try {bufferReader.close (); } catch (ioException ex) {j2Log (null, null, ex); }}} liste de retour; } / ** * Cat Read Text Fichier * * @param filepath * @param filename * @return * / public static byte [] cat (String filepath, string filename) {byte [] output = {}; try {String fullPath = combinePath (filepath, filename); Fichier fichier = nouveau fichier (fullpath); output = filetools.cat (fichier); } catch (exception ex) {j2Log (null, null, ex); } return output; } / ** * Fichier texte de lecture de chat * * @param fullPath * @return * / public static byte [] cat (String fullPath) {byte [] output = {}; try {file file = new File (fullPath); output = filetools.cat (fichier); } catch (exception ex) {j2Log (null, null, ex); } return output; } / ** * Fichier texte de lecture Cat * * @param fichier * @return * / public static octet [] cat (fichier de fichier) {inputStream dans = null; byte [] output = {}; essayez {if (! file.exists () || file.isdirectory ()) {lancez new FileToTFoundException (); } String fullpath = file.getPath (); longueur long = du (fichier, false); long _2m = 2097152; octet [] octets = nouveau octet [(int) longueur]; in = new FileInputStream (fullPath); pour (int count = 0; count! = -1;) {if (longueur> 16 * _2m) {longueur = 4 * _2m; } else if (longueur> 8 * _2m) {longueur = 2 * _2m; } else if (longueur> 4 * _2m) {longueur = _2m; } else if (longueur> 2 * _2m) {longueur = _2m / 2; } else if (longueur> _2m) {longueur = _2m / 4; } else {longueur = 4096; } bytes = new byte [(int) longueur]; count = in.read (octets); sortie = concatArray (octets, sortie); longueur = in.Available (); }} catch (exception ex) {j2Log (null, null, ex); } enfin {if (null! = in) {try {in.close (); } catch (exception ex) {j2Log (null, null, ex); }}} return output; } / ** * Merge Array * * @param old1 * @param old2 * @return * / private static byte [] concatArray (byte [] old1, byte [] old2) {byte [] newArray = new Byte [old1.length + old2.length]; System.ArrayCopy (Old1, 0, NewArray, 0, Old1.Length); System.ArrayCopy (Old2, 0, NewArray, Old1.Length, Old2.Length); retourner NewArray; } / ** * dd write file FullPath Content Content * * @param filepath * @param filename * @param contenu * @param isappend * / public static void dd (string filepath, string filename, byte [] contenu, boolean isaptend) {try {string fullPath = combinepath (filepath, filename); Fichier fichier = nouveau fichier (fullpath); FileTools.dd (fichier, contenu, isAPPEnd); } catch (exception ex) {j2Log (null, null, ex); }} / ** * dd write file FullPath Content Content * * @param fullPath * @param Content * @param isApend * / public static void dd (String fullPath, byte [] contenu, boolean isappend) {try {file file = new File (fullPath); FileTools.dd (fichier, contenu, isAPPEnd); } catch (exception ex) {j2Log (null, null, ex); }} / ** * dd write file FullPath Contenu Contenu * * @param fichier * @param contenu * @param isappend * / public static void dd (fichier file, byte [] contenu, boolean isappend) {fileoutputStream fileoutputStream = null; essayez {if (! file.exists ()) {file.createenewfile (); } fileOutputStream = new FileOutputStream (fichier, isApend); fileoutputStream.write (contenu); } catch (exception ex) {j2Log (null, null, ex); } enfin {try {if (null! = fileoutputStream) {fileoutputStream.close (); }} catch (ioException ex) {j2Log (null, null, ex); }}}} / ** * Écriture de contenu de fichier d'écriture pour fichier fullpath * * @param filepath * @param filename * @param contenu * / public static void write (String filepath, string fileName, string contenu) {try {String fullPath = combinePath (filepath, fileName); Fichier fichier = nouveau fichier (fullpath); Filetools.write (fichier, contenu, true); } catch (exception ex) {j2Log (null, null, ex); }} / ** * Écrivez du contenu de fichier Write File dans File FullPath * * @param fullPath * @param Content * / public static void write (String fullPath, String Content) {try {file file = new File (fullPath); Filetools.write (fichier, contenu, true); } catch (exception ex) {j2Log (null, null, ex); }} / ** * Écrivez du contenu de fichier Write Fichier dans le fichier fullPath * * @param fichier * @param contenu * / public static void write (fichier file, string contenu) {try {filetools.write (fichier, contenu, true); } catch (exception ex) {j2Log (null, null, ex); }} / ** * write write (append) contenu de fichier pour fichier fullpath * * @param filepath * @param filename * @param contenu * @param isappend * / public static void write (string filepath, string filename, string contenu, boolean isAPPEnd) {try {String fullPath = combinepath (filepath, filename); Fichier fichier = nouveau fichier (fullpath); FileTools.Write (fichier, contenu, isAPPEnd); } catch (exception ex) {j2Log (null, null, ex); }} / ** * write write (append) contenu de fichier pour fichier fullpath * * @param fullpath * @param contenu * @param isappend * / public static void write (string fullPath, String Content, boolean isaptend) {try {file file = new File (fullPath); FileTools.Write (fichier, contenu, isAPPEnd); } catch (exception ex) {j2Log (null, null, ex); }} / ** * write write (append) contenu de fichier pour fichier fullPath * * @param file * @param contenu * @param isappend * / public static void write (fichier file, string contenu, boolean isappend) {filewriter filewriter = null; essayez {if (! file.exists ()) {file.createenewfile (); } filewriter = new FileWriter (file.getPath (), isApend); filewriter.write (contenu); } catch (exception ex) {j2Log (null, null, ex); } enfin {if (null! = filewriter) {try {filewriter.close (); } catch (ioException ex) {j2Log (null, null, ex); }}}}} / ** * Tail Ajouter du contenu de contenu de fichier à l'emplacement d'index du fichier * * @param filepath * @param nom de fichier * @param contenu * @param index * / public static void tail (string filepath, string filepath, string contenue, long index) {try {string fullPath = combinepath (filepath, filename); Fichier fichier = nouveau fichier (fullpath); Filetools.tail (fichier, contenu, index); } catch (exception ex) {j2Log (null, null, ex); }} / ** * Tail Ajouter du contenu de contenu de fichier à l'emplacement d'index du fichier * * @param fullPath * @param Contenu * @param index * / public static void tail (String fullPath, String Content, long index) {try {file file = new File (fullPath); Filetools.tail (fichier, contenu, index); } catch (exception ex) {j2Log (null, null, ex); }} / ** * Tail Ajouter un contenu de contenu de fichier à l'emplacement d'index du fichier * * @param fichier * @param contenu * @param index * / public static void tail (fichier file, string contenu, long index) {randomaccessfile randomaccessfile = null; essayez {if (! file.exists ()) {file.createenewfile (); } randomaccessfile = new randomaccessfile (file.getPath (), "rw"); RandomAccessFile.seek (index); randomaccessfile.writeBytes (contenu); } catch (exception ex) {j2Log (null, null, ex); } enfin {if (null! = randomaccessfile) {try {randomaccessfile.close (); } catch (exception ex) {j2Log (null, null, ex); }}}}} / ** * Mkdir Create Directory * * @param filepath * @param filename * @return * / public static File Mkdir (String filepath, string filename) {file file = null; try {String fullPath = combinePath (filepath, filename); file = nouveau fichier (fullpath); file = mkdir (fichier); } catch (exception ex) {j2Log (null, null, ex); } return fichier; } / ** * Mkdir Create Directory * * @param fullPath * @return * / public static File mkdir (String fullPath) {file file = null; try {file = new File (fullPath); file = mkdir (fichier); } catch (exception ex) {j2Log (null, null, ex); } return fichier; } / ** * Mkdir Create Directory * * @param fichier * @return * / public static Fichier Mkdir (fichier file) {try {if (! file.exists ()) {file.mkdir (); // créer si le dossier n'existe pas}} catch (exception ex) {j2Log (null, null, ex); } return fichier; } / ** * Touch Create File * * @param filepath * @param filename * / public static void touch (string filepath, string filename) {try {string fullPath = combinePath (filepath, filename); Fichier fichier = nouveau fichier (fullpath); Touch (fichier); } catch (exception ex) {j2Log (null, null, ex); }} / ** * Touch Create File * * @param fullPath * / public static void touch (String fullPath) {try {file file = new File (fullPath); Touch (fichier); } catch (exception ex) {j2Log (null, null, ex); }} / ** * Touch Create File * * @param File * / public static void touch (file file) {try {if (! file.exists ()) {file.createewfile (); // créer si le fichier n'existe pas}} catch (exception ex) {j2Log (null, null, ex); }} / ** * rm Supprimer le fichier * * @param filepath * @param filename * / public static void rm (string filepath, string filename) {try {string fullPath = combinePath (filepath, filename); Fichier fichier = nouveau fichier (fullpath); rm (fichier); } catch (exception ex) {j2Log (null, null, ex); }} / ** * RM Delete File * * @param fullPath * / public static void rm (String fullPath) {try {file file = new File (fullPath); rm (fichier); } catch (exception ex) {j2Log (null, null, ex); }} / ** * RM Delete Fichier * * @param File * / public static void rm (fichier file) {try {if (! file.exists ()) {throw new filenotFoundException (); } if (file.isfile ()) {file.delete (); }} catch (exception ex) {j2Log (null, null, ex); }} / ** * RMDIR DESTION DIRECTORY * * @param filepath * @param filename * @param loop * / public static void rmdir (String filepath, string filename, boolean loop) {try {String fullPath = combinePath (filepath, filename); Fichier dir = nouveau fichier (fullpath); rmdir (dir, boucle); } catch (exception ex) {j2Log (null, null, ex); }} / ** * RMDIR DESTION DIRECTORY * * @param fullPath * @param LOOP * / public static void rmdir (String fullPat, Boolean Loop) {try {file dir = new File (fullPath); rmdir (dir, boucle); } catch (exception ex) {j2Log (null, null, ex); }} / ** * RMDIR DESTION DIRECTORY * * @param dir * @param LOOP * / public static void rmdir (fichier dir, booléan Loop) {try {if (! dir.exists ()) {throw new filenotFoundException (); } if (dir.isdirectory ()) {file [] files = dir.listFiles (); int length = files.length; pour (int i = 0; i <longueur && LOOP; i ++) {if (files [i] .isDirectory ()) {rmdir (fichiers [i], loop); } else {rm (fichiers [i]); }} if (loop || longueur == 0) {dir.delete (); }}} catch (exception ex) {j2Log (null, null, ex); }} / ** * DU Obtenez la taille réelle du fichier * * @param filepath * @param nom de fichier * @param boucle * @return * / public static long du (String filepath, String FileName, boolean Loop) {long size = 0; try {String fullPath = combinePath (filepath, filename); Fichier fichier = nouveau fichier (fullpath); size = du (fichier, boucle); } catch (exception ex) {j2Log (null, null, ex); } taille de retour; } / ** * DU Obtenez la taille réelle du fichier * * @param filepath * @param filename * @return * / public static long du (String filepath, string filename) {long size = 0; try {String fullPath = combinePath (filepath, filename); Fichier fichier = nouveau fichier (fullpath); size = du (fichier, false); } catch (exception ex) {j2Log (null, null, ex); } taille de retour; } / ** * DU Obtenez la taille réelle du fichier * * @param fullpath * @return * / public static long du (String fullPath) {long size = 0; try {file file = new File (fullPath); size = du (fichier, false); } catch (exception ex) {j2Log (null, null, ex); } taille de retour; } / ** * DU Obtenez la taille réelle du fichier * * @param fichier * @return * / public static long du (fichier file) {long size = 0; essayez {size = du (fichier, false); } catch (exception ex) {j2Log (null, null, ex); } taille de retour; } / ** * DU Obtenez la taille réelle du fichier * * @param fullpath * @param boucle * @return * / public static long du (String fullpath, booléan boucle) {long size = 0; try {file file = new File (fullPath); size = du (fichier, boucle); } catch (exception ex) {j2Log (null, null, ex); } taille de retour; } / ** * DU Obtenez la taille réelle du fichier * * @param fichier * @param boucle * @return * / public static long du (fichier de fichier, boucle booléenne) {filechannel fileChannel = null; taille longue = 0; essayez {if (! file.exists ()) {lancer un nouveau filenotfoundException (); } if (file.isfile ()) {fileInputStream fis = new FileInputStream (file); FileChannel = fis.getChannel (); size = filechannel.size (); } else if (file.isdirectory ()) {file [] file = file.listFiles (); int length = files.length; pour (int i = 0; i <longueur && LOOP; i ++) {if (files [i] .isDirectory ()) {du (fichiers [i], loop); } else {size + = du (fichiers [i], false); }}}} catch (exception ex) {j2Log (null, null, ex); } enfin {if (null! = fileChannel) {try {filechannel.close (); } catch (exception ex) {j2Log (null, null, ex); }}} taille de retour; }}Ce qui précède concerne cet article, j'espère qu'il sera utile à l'apprentissage de tout le monde.