Recentemente, para modificar os nomes de arquivos de um grande número de dramas americanos favoritos, escrevi um widget usando o swing. O código é a peça de processamento de arquivos. O conteúdo específico é o seguinte
pacote datei.steuern; importar java.io.bufferedReader; importar java.io.file; importar java.io.fileInputStream; importar java.io.fileNotFoundException; importar java.io.fileOuttStream; importin.io.io.filewriter; import java.io.ioxception; importação; java.io.randomaccessfile; importar java.nio.bytebuffer; importar java.nio.channels.FileChannel; importar java.util.arraylist; import java.util.list; importrays; import java.ut.utlections; importação.util.list; importrays; java.util.logging.logger; /** * * @author s.watson */public class filetools {public fileTools () {}/** * formatPath Escape File Directory * * @param caminho * @return */public static string formatPath (string path) {retorna path.replaceall ("//////"/"); } / ** * CombinePath File Path Merge * * @param eins * @param zwei * @return * / String estática privada 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 ("//", "/"); } retornar Dori; } / ** * LISTA2Array List Array * * @param list * @return * / string estática privada [] list2Array (lista da lista) {string array [] = (string []) list.toarray (new string [list.size ()]); matriz de retorno; } / ** * Arquivo de cópia cp * * @param fonte * @param Destination * @param loop * @return * / public static list <File> cp (fonte da string, destino da string, loop booleano) {list <File> list = new ArrayList (); tente {arquivo srcfile = new File (origem); Arquivo desfile = novo arquivo (destino); list.addall (cp (srcfile, desfile, loop)); } catch (exceção ex) {j2log (null, null, ex); } Lista de retorno; } / ** * Arquivo de cópia cp * * @param fonte * @param Destination * @param loop * @return * / public static list <File> cp (fonte do arquivo, destino do arquivo, loop booleano) {list <File> list = new ArrayList (); tente {if (! source.exists () || source.isdirectory ()) {lança novo fileNotfoundException (); } list.add (cp (fonte, destino)); if (loop) {string [] subfile = fonte.list (); para (String Subpath: Subfily) {String src = CombinePath (Source.getPath (), Subpath); // O caminho original do arquivo da String subfile des = CombinePath (destination.getPath (), Subpath); // o caminho de destino do arquivo subfile subfile = novo arquivo (SRC); if (subfile.isfile ()) {list.add (cp (src, des)); } else if (subfile.isdirectory () && loop) {list.addall (cp (src, des, loop)); }}}}} catch (Exceção ex) {j2log (null, null, ex); } Lista de retorno; } / ** * Arquivo de copiar de arquivo único cp * * @param fonte * @param Destination * @return * / public static file cp (fonte da string, destino de string) {arquivo desfile = null; tente {arquivo srcfile = new File (origem); DESFILE = novo arquivo (destino); DESFILE = CP (SRCFILE, DESFILE); } catch (exceção ex) {j2log (null, null, ex); } retornar desfile; } / ** * Arquivo de copiar de arquivo único cp * * @param fonte * @param Destination * @return * / public static arquivo cp (fonte de arquivo, destino de arquivo) {fileInputStream em = null; FileOutputStream Out = NULL; Filechannel inc = nulo; Filechannel outc = null; tente {if (! source.exists () || source.isdirectory ()) {lança novo fileNotfoundException (); } if (fonte.getPath (). Equals (destino.getPath ())) {return fonte; } long allbytes = du (fonte, false); if (! Destination.Exists ()) {Destination.CreateNewFile (); } in = new FileInputStream (fonte.getPath ()); out = new FileOutputStream (destino); Inc = in.getChannel (); outc = out.getChannel (); Bytebuffer bytebuffer = null; comprimento longo = 2097152; // tamanho básico, padrão de 2m de comprimento _2m = 2097152; enquanto (Inc.Position () <Inc.Size ()) {if (allbytes> (64 * length)) {// Se o tamanho do arquivo for maior que 128m, o cache será alterado para 64m de comprimento = 32 * _2m; } else if (allbytes> (32 * comprimento)) {// Se o tamanho do arquivo for maior que 64m, o cache será alterado para 32m de comprimento = 16 * _2m; } else if (allbytes> (16 * comprimento)) {// Se o tamanho do arquivo for maior que 32m, o cache será alterado para 16m de comprimento = 8 * _2m; } else if (allbytes> (8 * comprimento)) {// Se o tamanho do arquivo for maior que 16m, o cache será alterado para 8m de comprimento = 4 * _2m; } else if (allbytes> (4 * comprimento)) {// Se o tamanho do arquivo for maior que 8m, o cache será alterado para 4m de comprimento = 2 * _2m; } else if (allbytes> (2 * comprimento)) {// Se o tamanho do arquivo for maior que 4m, o cache será alterado para 2m comprimento = _2m; } else if (allbytes> (comprimento)) {// Se o tamanho do arquivo for maior que 2m, o cache será alterado para 1m comprimento = _2m/2; } else if (allBytes <length) {// se o arquivo for menor que o tamanho da base, comprimento de saída diretamente = allBytes; } allBytes = inc.size () - Inc.Position (); byTeBuffer = bytebuffer.alocatedirect ((int) comprimento); Inc.read (bytebuffer); byteBuffer.flip (); outc.write (bytebuffer); outc.force (false); }} catch (Exceção ex) {j2log (null, null, ex); } finalmente {tente {if (null! = Inc) {inc.close (); } if (null! = outc) {outc.close (); } if (null! = in) {in.close (); } if (null! = out) {out.close (); }} catch (Exceção ex) {j2log (null, null, ex); }} retornar destino; } / ** * Renomear o arquivo renomeado * * @param filepath * @param de * @param para * @return * / public static file renome (string filepath, string de, string para) {file newfile = null; tente {file OldFile = new File (combainPath (filepath, de)); newfile = novo arquivo (combainpath (filepath, para)); renomear (newfile, OldFile); } catch (exceção ex) {j2log (null, null, ex); } retornar newFile; } / ** * Renomeie o arquivo renomeado * * @param para * @param de * @return * / public static arquivo renomear (arquivo de, arquivo para) {try {string newpath = to.getPath (); String oldPath = de.getPath (); if (! OldPath.Equals (newPath)) {if (! to.exists ()) {from.renameto (to); }}} catch (Exceção ex) {j2log (null, null, ex); } retornar a; } / ** * mv mover arquivo * * @param fileName * @param fonte * @param Destination * @param tampa * / public static void mv (nome do arquivo de string, origem da string, destino da string, capa booleana) {Try (! Arquivo newfile = novo arquivo (combainpath (destino, nome do arquivo)); MV (OldFile, NewFile, capa); }} catch (Exceção ex) {j2log (null, null, ex); }} / ** * mv mover arquivo * * @param fonte * @param Destination * @param capa * / public static void mv (fonte da string, destino da string, capa booleana) {try {if (! Source.equals (destino)) {file OldFile = new File (fonte); Arquivo newFile = novo arquivo (destino); MV (OldFile, NewFile, capa); }} catch (Exceção ex) {j2log (null, null, ex); }} / ** * mv mover arquivo * * @param fonte * @param Destination * @param capa * / public static void mv (fonte de arquivo, destino de arquivo, capa booleana) {try {if (! source.exists ()) {tiro o novo fileNotFoundException (); } Stringbuilder filename = new StringBuilder (fonte.getName ()); if (! Cover && source.getPath (). Equals (destino.getPath ())) {if (filename.indexof (".")> 0) {filename.insert (nome do arquivo.lastIndexOf ("."), "_copy"); } else {filename.append ("_ cópia"); } cp (origem, novo arquivo (combainpath (fonte.getParent (), filename.toString ()))); } else {fonte.renameto (destino); }} catch (Exceção ex) {j2log (null, null, ex); }} / ** * Extensões Obtenha informações de extensão de arquivo * * @param filepath * @param filename * @return * / string estática privada [] extensões (string filepath, string filename) {string [] extension = {}; tente {string fullpath = combinathpath (filepath, nome do arquivo); Arquivo de arquivo = novo arquivo (FullPath); extensões (arquivo); } catch (exceção ex) {j2log (null, null, ex); } retornar extensão; } / ** * Extensões Obtenha informações de extensão de arquivo * * @param fullpath * @return * / string estática privada [] Extensões (string fullPath) {string [] extension = {}; tente {arquivo file = new File (FullPath); extensões (arquivo); } catch (exceção ex) {j2log (null, null, ex); } retornar extensão; } / ** * Extensões Obtenha informações de extensão do arquivo * * @param arquivo * @return * / string estática privada [] Extensões (arquivo de arquivo) {string [] extension = {}; tente {if (file.isfile ()) {string filename = file.getName (); if (filename.lastIndexOf (".")> = 0) {int lastIndex = filename.lastIndexof ("."); extensão [0] = string.valueof (lastIndex); // índice de "." extensão [1] = nome do arquivo.substring (lastIndex + 1); // extensão de extensão [2] = nome do arquivo.substring (0, lastIndex); // nome do arquivo}}} catch (exceção ex) {j2log (null, null, ex); } retornar extensão; } / ** * ls atravessando o arquivo * * @param filepath * @param loop * @return * / list estática pública <File> ls (string filepath, loop booleano) {list <File> list = new ArrayList (); tente {arquivo file = novo arquivo (filepath); list.addall (ls (arquivo, loop)); } catch (exceção ex) {j2log (null, null, ex); } Lista de retorno; } / ** * ls atravessando o arquivo * * @param arquivo * @param loop * @return * / public static list <File> ls (arquivo de arquivo, loop boolean) {list <File> list = new ArrayList (); tente {list.add (arquivo); if (! file.isdirectory ()) {list.add (arquivo); } else if (file.isdirectory ()) {file [] sublist = file.listfiles (); sublist = filessort (sublista, verdadeiro); para (subfile de arquivo: sublist) {if (subfile.isdirectory () && loop) {list.addall (ls (subfile.getpath (), loop)); } else {list.add (subfile); }}}} catch (Exceção ex) {j2log (null, null, ex); } Lista de retorno; } / ** * FILESSORT FILE CLENT (Ordem ascendente padrão) * * @param parentPath * @param sublist * @return * / arquivo estático privado [] filessort (file [] infiles, boolean ASC) {list <string> arquivos = new ArrayList (); Lista <String> dirs = new ArrayList (); para (Subfile de Arquivo: Infiles) {if (subfile.isdirectory ()) {dirs.add (subfile.getPath ()); } else if (subfile.isfile ()) {files.add (subfile.getpath ()); }} String [] filearray = {}; if (files.size ()> 0) {filearray = list2Array (arquivos); Arrays.sort (FileArray); if (! ASC) {Arrays.sort (FileArray, Coleções.verseverOrder ()); }} String [] dirArray = {}; if (Dirs.size ()> 0) {dirArray = list2Array (dirs); Arrays.sort (DirArray); if (! ASC) {Arrays.sort (dirArray, collection.verseverOrder ()); }} retornar concat2FileArray (FileArray, DirArray); } / ** * Concat2FileArray Merge Arquivo Array * * @param antigo1 * @param Old2 * @return * / arquivo estático privado [] concat2fileArray (string [] old1, string [] antigo2) {file [] newArray = new File [Old1.Length + Old2.Length]; for (int i = 0, n = antigo1.length; i <n; i ++) {newArray [i] = novo arquivo (antigo1 [i]); } para (int i = 0, j = antigo1.length, n = (antigo1.length+Old2.Length); j <n; i ++, j ++) {newArray [j] = novo arquivo (antigo2 [i]); } retornar newArray; } / ** * Leia o arquivo de texto * * @param filepath * @param filename * @param charset * @return * / public static stringbuilder Read (string filepath, string filename, string charset) {stringbuilder sb = new stringbuilder (); tente {string fullpath = combinathpath (filepath, nome do arquivo); Arquivo de arquivo = novo arquivo (FullPath); sb.append (filetools.tail (arquivo, false, 0, charset)); } catch (exceção ex) {j2log (null, null, ex); } retornar sb; } / ** * Leia o arquivo de texto * * @param fullpath * @param charset * @return * / public static stringbuilder read (string fullpath, string charset) {stringbuilder sb = new stringbuilder (); tente {arquivo file = new File (FullPath); sb.append (filetools.tail (arquivo, false, 0, charset)); } catch (exceção ex) {j2log (null, null, ex); } retornar sb; } / ** * Leia o arquivo de texto * * @param arquivo * @param charset * @return * / public static stringbuilder Read (arquivo de arquivo, string charset) {StringBuilder sb = new StringBuilder (); tente {sb.append (filetools.tail (arquivo, false, 0, charset)); } catch (exceção ex) {j2log (null, null, ex); } retornar sb; } / ** * Encontre leia a linha especificada do arquivo de texto * * @param filepath * @param filename * @param linha * @param charset * @return * / public static stringbuilder find (string filepath, string filename, int line, string charset) {stringbuilder sb = new stringbuilder (); tente {string fullpath = combinathpath (filepath, nome do arquivo); Arquivo de arquivo = novo arquivo (FullPath); sb.append (filetools.tail (arquivo, true, linha, charset)); } catch (exceção ex) {j2log (null, null, ex); } retornar sb; } / ** * Encontre leia a linha especificada do arquivo de texto * * @param fullpath * @param linha * @param charset * @return * / public static stringbuilder find (string fullPath, int line, string charset) {stringbuilder sb = new stringbuilder (); tente {arquivo file = new File (FullPath); sb.append (filetools.tail (arquivo, true, linha, charset)); } catch (exceção ex) {j2log (null, null, ex); } retornar sb; } / ** * Encontre leia a linha especificada do arquivo de texto * * @param arquivo * @param linha * @param charset * @return * / public static stringbuilder find (arquivo de arquivo, int, linha string string) {stringbuilder sb = new StringBuilder (); tente {sb.append (filetools.tail (arquivo, true, linha, charset)); } catch (exceção ex) {j2log (null, null, ex); } retornar sb; } / ** * Tail Leia o arquivo de texto * * @param filepath * @param filename * @param charset * @param encontre * @param linha * @return * / public static stringbuilder cauda (string filepath, string filename, boolean find, int line, string charset) {stringbuilder sb = stringbuilder; tente {string fullpath = combinathpath (filepath, nome do arquivo); Arquivo de arquivo = novo arquivo (FullPath); sb.append (filetools.tail (arquivo, localização, linha, charset)); } catch (exceção ex) {j2log (null, null, ex); } retornar sb; } / ** * Arquivo de texto de leitura da cauda * * @param fullpath * @param charset * @param find * @param line * @return * / public static stringbuilder cauda (string fullpath, boolean encontrado, linha int, string charset) {stringbuilder sb = new stringbuilder (); tente {arquivo file = new File (FullPath); sb.append (filetools.tail (arquivo, localização, linha, charset)); } catch (exceção ex) {j2log (null, null, ex); } retornar sb; } / ** * Arquivo de texto de leitura da cauda * * Arquivo @param * @param charset * @param find * @param linha * @return * / public static stringbuilder cauda (arquivo de arquivo, boolean find, int line, string charset) {stringbuilder sb = new stringbuilder (); BufferReader bufferReader = NULL; if (null == charset || "" .equals (charset)) {charset = "utf-8"; } tente {if (! file.exists () || file.isdirectory ()) {lança novo fileNotfoundException (); } String fullPath = file.getPath (); BufferReader = new BufferredReader (new InputStreamReader (new FileInputStream (Fullpath), Charset)); Temp de string; para (int i = 0; (temp = bufferReader.readline ())! = null; i ++) {if (! find || line == i) {sb.append (temp); }}} catch (Exceção ex) {j2log (null, null, ex); } finalmente {if (null! = bufferReader) {try {bufferreader.close (); } catch (ioexception ex) {j2log (null, null, ex); }}} retorna sb; } / ** * sed Arquivo de texto de leitura * * @param filepath * @param fileName * @param charset * @return * / list estática pública <string> sed (string filepath, string filename, string charset) {list <string> list = new ArrayList (); tente {string fullpath = combinathpath (filepath, nome do arquivo); Arquivo de arquivo = novo arquivo (FullPath); list.addall (filetools.sed (arquivo, charset)); } catch (exceção ex) {j2log (null, null, ex); } Lista de retorno; } / ** * sed Arquivo de texto de leitura * * @param fullpath * @param charset * @return * / public static list <tring> sed (string fullpath, string charset) {list <string> list = new ArrayList (); tente {arquivo file = new File (FullPath); list.addall (filetools.sed (arquivo, charset)); } catch (exceção ex) {j2log (null, null, ex); } Lista de retorno; } / ** * SED LEIA FILE DE TEXTO * * @PARAM FILE * @param charset * @return * / public static list <tring> sed (arquivo de arquivo, string charset) {list <string> list = new ArrayList (); BufferReader bufferReader = NULL; if (null == charset || "" .equals (charset)) {charset = "utf-8"; } tente {if (! file.exists () || file.isdirectory ()) {lança novo fileNotfoundException (); } String fullPath = file.getPath (); BufferReader = new BufferredReader (new InputStreamReader (new FileInputStream (Fullpath), Charset)); Temp de string; for (int i = 0; (temp = bufferReader.readline ())! = null; i ++) {list.add (temp); }} catch (Exceção ex) {j2log (null, null, ex); } finalmente {if (null! = bufferReader) {try {bufferreader.close (); } catch (ioexception ex) {j2log (null, null, ex); }}} Lista de retorno; } / ** * Arquivo de texto de leitura do gato * * @param filepath * @param filename * @return * / public static byte [] cat (string filepath, string filename) {byte [] output = {}; tente {string fullpath = combinathpath (filepath, nome do arquivo); Arquivo de arquivo = novo arquivo (FullPath); output = fileTools.cat (arquivo); } catch (exceção ex) {j2log (null, null, ex); } retornar saída; } / ** * Arquivo de texto de leitura do gato * * @param fullpath * @return * / public static byte [] cat (string fullpath) {byte [] output = {}; tente {arquivo file = new File (FullPath); output = fileTools.cat (arquivo); } catch (exceção ex) {j2log (null, null, ex); } retornar saída; } / ** * Arquivo de texto de leitura do gato * * arquivo @param * @return * / public static byte [] cat (arquivo de arquivo) {inputStream em = null; byte [] output = {}; tente {if (! file.exists () || file.isdirectory ()) {lança novo fileNotfoundException (); } String fullPath = file.getPath (); comprimento longo = du (arquivo, false); longo _2m = 2097152; byte [] bytes = novo byte [(int) comprimento]; in = new FileInputStream (FullPath); for (int conting = 0; count! = -1;) {if (comprimento> 16 * _2m) {length = 4 * _2m; } else if (comprimento> 8 * _2m) {comprimento = 2 * _2m; } else if (comprimento> 4 * _2m) {comprimento = _2m; } else if (comprimento> 2 * _2m) {length = _2m / 2; } else if (comprimento> _2m) {comprimento = _2m / 4; } else {comprimento = 4096; } bytes = novo byte [(int) comprimento]; count = in.read (bytes); saída = concatArray (bytes, saída); comprimento = in.Available (); }} catch (Exceção ex) {j2log (null, null, ex); } finalmente {if (null! = in) {tente {in.close (); } catch (exceção ex) {j2log (null, null, ex); }}} retornar saída; } / ** * Merge Array * * @param Old1 * @param Old2 * @return * / byte estático privado [] concatArray (byte [] old1, byte [] old2) {byte [] newarray = new byte [antigo1.length + Old2.Length]; System.arraycopy (Old1, 0, NewArray, 0, Old1.Length); System.ArrayCopy (Old2, 0, NewArray, Old1.Length, Old2.Length); retornar newarray; } / ** * DD Write Arquivo Conteúdo do FullPath Conteúdo * * @param filepath * @param FileName * @param Content * @param isappend * / public static void dd (string filepath, string filename, byte [] conteúdo, boolean isappend) {try {string fullPathPathPath (filepath, filename); Arquivo de arquivo = novo arquivo (FullPath); FileTools.dd (arquivo, conteúdo, isappend); } catch (exceção ex) {j2log (null, null, ex); }} / ** * DD Write Arquivo Conteúdo do FullPath Content * * @param fullPath * @param Content * @param isappend * / public static void dd (string fullpath, byte [] content, boolean iSappend) {Try FileTools.dd (arquivo, conteúdo, isappend); } catch (exceção ex) {j2log (null, null, ex); }} / ** * DD Write Arquivo Conteúdo do FullPath Content * * @param arquivo * @param Content * @param isappend * / public static void dd (arquivo de arquivo, byte [] content, boolean isappend) {fileOutputStream fileOutstream = null; tente {if (! file.exists ()) {file.createNewfile (); } fileOutputStream = new FileOutputStream (FILE, ISAPPEND); FileOutputStream.Write (Content); } catch (exceção ex) {j2log (null, null, ex); } finalmente {try {if (null! = FileOutputStream) {FileOutputStream.close (); }} catch (ioexception ex) {j2log (null, null, ex); }}}} / ** * Escreva conteúdo do arquivo de gravação para arquivo fullpath * * @param filepath * @param filename * @param Content * / public static void write (string filepath, string filename, string content) {try {string fullPath = combinath (filepath, filename); Arquivo de arquivo = novo arquivo (FullPath); Filetools.write (arquivo, conteúdo, true); } catch (exceção ex) {j2log (null, null, ex); }} / ** * Write Arquivo Conteúdo do arquivo fullpath * * @param fullpath * @param content * / public static void write (string fullpath, string content) {try {file file = new File (fullPath); Filetools.write (arquivo, conteúdo, true); } catch (exceção ex) {j2log (null, null, ex); }} / ** * Write Arquivo Conteúdo do arquivo FullPath * * @param arquivo * @param Content * / public static void write (arquivo de arquivo, string content) {try {filetools.write (arquivo, conteúdo, true); } catch (exceção ex) {j2log (null, null, ex); }} / ** * Write Write (Append) Conteúdo do arquivo para arquivar FullPath * * @param filepath * @param filename * @param Content * @param isappend * / public static void write (string filepath, string filename, string content, boolean isappend) {{string fullpathPathPath (filepath, filename; Arquivo de arquivo = novo arquivo (FullPath); FileTools.write (arquivo, conteúdo, isappend); } catch (exceção ex) {j2log (null, null, ex); }} / ** * Write Write (Append) Conteúdo do arquivo para arquivo fullpath * * @param fullPath * @param Content * @param isappend * / public static void write (string fullpath, string content, boolean isappend) {try {file file = new File (fullPath); FileTools.write (arquivo, conteúdo, isappend); } catch (exceção ex) {j2log (null, null, ex); }} / ** * Write Write (Append) Conteúdo do arquivo para arquivo fullpath * * @param arquivo * @param Content * @param isappend * / public static void write (arquivo de arquivo, conteúdo da string, boolean isappend) {fileWriter fileWriter = null; tente {if (! file.exists ()) {file.createNewfile (); } fileWriter = new FileWriter (File.getPath (), ISAPPEND); fileWriter.Write (Content); } catch (exceção ex) {j2log (null, null, ex); } finalmente {if (null! = fileWriter) {try {fileWriter.close (); } catch (ioexception ex) {j2log (null, null, ex); }}}}} / * * Arquivo de arquivo = novo arquivo (FullPath); Filetools.tail (arquivo, conteúdo, índice); } catch (exceção ex) {j2log (null, null, ex); }} / ** * Tail Adicionar conteúdo do arquivo ao conteúdo do índice do arquivo * * @param fullpath * @param Content * @param index * / public static void cauda (string fullpath, conteúdo da string, índice longo) {try {file file = new File (FullPath); Filetools.tail (arquivo, conteúdo, índice); } catch (exceção ex) {j2log (null, null, ex); }} / ** * Tail Adicionar conteúdo do arquivo ao conteúdo do índice do arquivo * * @param arquivo * @param Content * @param Índice * / public static void Tail (arquivo de arquivo, conteúdo da string, índice longo) {aleatomAccessFile RandomAccessFile = null; tente {if (! file.exists ()) {file.createNewfile (); } RandomAccessFile = new RandomAccessFile (file.getPath (), "rw"); RandomAccessFile.Seek (índice); RandomAccessFile.WriteBytes (conteúdo); } catch (exceção ex) {j2log (null, null, ex); } finalmente {if (null! } catch (exceção ex) {j2log (null, null, ex); }}}}} / ** * mkdir Criar diretório * * @param filepath * @param filename * @return * / public file static mkdir (string filepath, string filename) {arquivo file = null; tente {string fullpath = combinathpath (filepath, nome do arquivo); arquivo = novo arquivo (FullPath); arquivo = mkdir (arquivo); } catch (exceção ex) {j2log (null, null, ex); } retornar arquivo; } / ** * mkdir Criar diretório * * @param fullpath * @return * / public static arquivo mkdir (string fullPath) {arquivo file = null; tente {file = new File (FullPath); arquivo = mkdir (arquivo); } catch (exceção ex) {j2log (null, null, ex); } retornar arquivo; }/** * mkdir Criar diretório * * @param arquivo * @return */public estático arquivo mkdir (arquivo file) {try {if (! file.exists ()) {file.mkdir (); // criar se a pasta não existir}} catch (exceção ex) {j2Log (null, null, null, null, null, null, null, null, null, null, null, null); } retornar arquivo; } / ** * Touch Criar arquivo * * @param filepath * @param filename * / public static void touch (string filepath, string filename) {try {string fullPath = combinath (filepath, nome do arquivo); Arquivo de arquivo = novo arquivo (FullPath); toque (arquivo); } catch (exceção ex) {j2log (null, null, ex); }} / ** * Toque em Criar arquivo * * @param fullpath * / public static void touch (string fullpath) {try {file file = new File (FullPath); toque (arquivo); } catch (exceção ex) {j2log (null, null, ex); }}/** * Toque em Criar arquivo * * @param arquivo */public static void touch (arquivo de arquivo) {try {if (! file.exists ()) {file.createnewfile (); // criar se o arquivo não existir}} catch (exceção ex) {j2log (null, null, ex); }} / ** * rm exclua o arquivo * * @param filepath * @param filename * / public static void rm (string filepath, string filename) {try {string fullPath = combinathpath (filepath, nome do arquivo); Arquivo de arquivo = novo arquivo (FullPath); rm (arquivo); } catch (exceção ex) {j2log (null, null, ex); }} / ** * rm Excluir arquivo * * @param fullpath * / public static void rm (string fullPath) {try {file file = new File (FullPath); rm (arquivo); } catch (exceção ex) {j2log (null, null, ex); }} / ** * rm Excluir arquivo * * @param arquivo * / public static void rm (arquivo de arquivo) {try {if (! file.exists ()) {lança novo fileNotFoundException (); } if (file.isfile ()) {file.delete (); }} catch (Exceção ex) {j2log (null, null, ex); }} / ** * rmdir excluir diretório * * @param filepath * @param filename * @param loop * / public static void rmdir (string filepath, string filename, loop boolean) {try {string fullPath = combine (filepath, filename); Arquivo Dir = novo arquivo (FullPath); rmdir (dir, loop); } catch (exceção ex) {j2log (null, null, ex); }} / ** * rmdir excluir diretório * * @param fullpath * @param loop * / public static void rmdir (string fullpath, loop booleano) {try {file dir = new File (fullpath); rmdir (dir, loop); } catch (exceção ex) {j2log (null, null, ex); }} / ** * rmdir excluir diretório * * @param dir * @param loop * / public static void rmdir (arew dir, boolean loop) {try {if (! dir.exists ()) {tiro o novo fileNotFoundException (); } if (dir.isdirectory ()) {file [] arquivos = dir.ListFiles (); int length = files.length; para (int i = 0; i <comprimento && loop; i ++) {if (arquivos [i] .isdirectory ()) {rmdir (arquivos [i], loop); } else {rm (arquivos [i]); }} if (loop || comprimento == 0) {Dir.Delete (); }}} catch (Exceção ex) {j2log (null, null, ex); }} / ** * du obtém o tamanho real do arquivo * * @param filepath * @param filename * @param loop * @return * / public static long Du (string filepath, string filename, boolean loop) {tamanho longo = 0; tente {string fullpath = combinathpath (filepath, nome do arquivo); Arquivo de arquivo = novo arquivo (FullPath); tamanho = du (arquivo, loop); } catch (exceção ex) {j2log (null, null, ex); } tamanho de retorno; } / ** * du obtém o tamanho do arquivo real * * @param filepath * @param filename * @return * / public static long Du (string filepath, string filename) {tamanho longo = 0; tente {string fullpath = combinathpath (filepath, nome do arquivo); Arquivo de arquivo = novo arquivo (FullPath); tamanho = du (arquivo, false); } catch (exceção ex) {j2log (null, null, ex); } tamanho de retorno; } / ** * DU Obtenha o tamanho real do arquivo * * @param fullpath * @return * / public static long Du (string fullpath) {tamanho longo = 0; tente {arquivo file = new File (FullPath); tamanho = du (arquivo, false); } catch (exceção ex) {j2log (null, null, ex); } tamanho de retorno; } / ** * DU Obtenha o tamanho real do arquivo * * @param arquivo * @return * / public static long Du (arquivo de arquivo) {tamanho longo = 0; tente {size = du (arquivo, false); } catch (exceção ex) {j2log (null, null, ex); } tamanho de retorno; } / ** * du obtém o tamanho do arquivo real * * @param fullpath * @param loop * @return * / public static long Du (string fullpath, loop booleano) {tamanho longo = 0; tente {arquivo file = new File (FullPath); tamanho = du (arquivo, loop); } catch (exceção ex) {j2log (null, null, ex); } tamanho de retorno; } / ** * du obtém o tamanho do arquivo real * * arquivo @param * @param loop * @return * / public static long du (arquivo de arquivo, loop booleano) {filechannel filechannel = null; tamanho longo = 0; tente {if (! file.exists ()) {lança novo fileNotfoundException (); } if (file.isfile ()) {fileInputStream fis = new FileInputStream (File); filechannel = fis.getChannel (); size = filechannel.size (); } else if (file.isdirectory ()) {file [] files = file.listfiles (); int length = files.length; para (int i = 0; i <comprimento && loop; i ++) {if (arquivos [i] .isdirectory ()) {du (arquivos [i], loop); } else {size += du (arquivos [i], false); }}}} catch (Exceção ex) {j2log (null, null, ex); } finalmente {if (null! = filechannel) {tente {filechannel.close (); } catch (exceção ex) {j2log (null, null, ex); }}} Retornar tamanho; }}O exposto acima é tudo sobre este artigo, espero que seja útil para o aprendizado de todos.