最近、多数のお気に入りのアメリカのドラマのファイル名を変更するために、Swingを使用してウィジェットを書きました。コードはファイル処理パーツです。特定のコンテンツは次のとおりです
パッケージdatei.steuern; java.io.bufferedreader; Import java.io.file; import java.io.fileinputStream; Import java.io.fileenotfoundexception; Import java.io.fileOutputStream; Import java.io.filewriter; Import Java.ioException; Import java.io.Io.IonputStreeC java.io.randomaccessfile; Import java.nio.bytebuffer; Import java.nio.channels.filechannel; Import java.util.arraylist; Import java.util.arrays; import java.util.celections; import java.util.list; java.util.logging.logger; /** * * @author s.watson */public class filetools {public filetools(){}/** * formatpath escase file directory * * @param path * @return */public static string formatpath(string path){return path.replaceall( "/////"、 "/"); } / ** *コンバインパスパスマージ * * @param eins * @param zwei * @return * / private static string combine(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( "//"、 "/"); } doriを返します。 } / ** * list2Arrayリスト変換配列 * * @param list * @return * / private static string [] list2Array(list list){string array [] =(string [])list.toarray(new String [list.size()]);返品配列; } / ** * cpコピーファイル * * @param Source * @param destination * @param loop * @return * / public static list <file> cp(string source、string destination、boolean loop){list <file> list = new ArrayList(); try {file srcfile = new file(source);ファイルdesfile = new File(destination); list.addall(cp(srcfile、desfile、loop)); } catch(Exception ex){j2log(null、null、ex); }返品リスト。 } / ** * cpコピーファイル * * @param Source * @param Destination * @param loop * @return * / public static list <file> cp(file source、file destination、boolean loop){list <file> list = new ArrayList(); try {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 = combinepath(source.getpath()、subpath); //サブファイル文字列des = combine.getpath()、subpath); // subfileファイルのターゲットパスsubfile = new file(src); if(subfile.isfile()){list.add(cp(src、des)); } else if(subfile.isdirectory()&& loop){list.addall(cp(src、des、loop)); }}}}}} catch(例外Ex){j2log(null、null、ex); }返品リスト。 } / ** * cpシングルファイルコピーファイル * * @param Source * @param destination * @return * / public static file cp(string source、string destination){file desfile = null; try {file srcfile = new file(source); Desfile = new File(destination); Desfile = cp(srcfile、desfile); } catch(Exception ex){j2log(null、null、ex); } Desfileを返します。 } / ** * cpシングルファイルコピーファイル * * @param Source * @param destination * @return * / public static file cp(file source、file destination){fileinputStream in = null; fileoutputStream out = null; filechannel inc = null; filechannel outc = null; try {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.createNewfile(); } in = new FileInputStream(source.getPath()); out = new fileoutputStream(宛先); inc = in.getChannel(); outc = out.getChannel(); bytebuffer bytebuffer = null;長さ= 2097152; //基本サイズ、デフォルト2mの長さ_2m = 2097152; while(inc.position()<inc.size()){if(allbytes>(64 *長さ)){//ファイルサイズが128mを超える場合、キャッシュは64mの長さ= 32 * _2mに変更されます。 } else if(allbytes>(32 *長さ)){//ファイルサイズが64mを超える場合、キャッシュは長さ32mに変更されます= 16 * _2m; } else if(allbytes>(16 *長さ)){//ファイルサイズが32mを超える場合、キャッシュは長さ16mに変更されます= 8 * _2m; } else if(allbytes>(8 *長さ)){//ファイルサイズが16mを超える場合、キャッシュは長さ8mに変更されます= 4 * _2m; } else if(allbytes>(4 *長さ)){//ファイルサイズが8mを超える場合、キャッシュは4mの長さ= 2 * _2mに変更されます。 } else if(allbytes>(2 *長さ)){//ファイルサイズが4mを超える場合、キャッシュは2mの長さ= _2mに変更されます。 } else if(allbytes>(length)){//ファイルサイズが2mを超える場合、キャッシュは1mの長さ= _2m/2に変更されます。 } else if(allbytes <length){//ファイルがベースサイズよりも小さい場合、出力の長さは直接= allbytes; } allbytes = inc.size()-Inc.Position(); bytebuffer = bytebuffer.allocatedirect((int)length); Inc.Read(bytebuffer); bytebuffer.flip(); outc.write(bytebuffer); outc.force(false); }} catch(Exception ex){j2log(null、null、ex); }最後に{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); }}宛先を返します。 } / ** * file rename * * @param filepath * @param from * @param to * / public static file rename(string filepath、string from、string to){file newfile = null; try {file oldfile = new file(combinpath(filepath、from)); newfile = new file(combainpath(filepath、to));名前を変更(NewFile、OldFile); } catch(Exception ex){j2log(null、null、ex); } newFileを返します。 } / ** * rename file rename * * @param to * @param from * @return * / public static file rename(file from、file to){try {string newpath = to.getPath();文字列oldpath = from.getPath(); if(!oldpath.equals(newpath)){if(!to.exists()){from.renameto(to); }}} catch(Exception ex){j2log(null、null、ex); }に戻ります。 } / ** * MV Move File * * @Param Filename * @Param Source * @Param Destination * @Param Cover * / public Static Void MV(String Filename、String Source、String Destination、Boolean Cover){try {if(!source.equals){file oldfile = new file(combainpath(filename));ファイルnewFile = new File(CombainPath(宛先、ファイル名)); MV(OldFile、NewFile、カバー); }} catch(Exception ex){j2log(null、null、ex); }} / ** * MV Move File * * @Param Source * @Param Destination * @Param Cover * / public static void MV(String Source、String Destination、Boolean Cover){try {if(!source.equals(destination)){file oldfile = new file(source);ファイルnewFile = newファイル(宛先); MV(OldFile、NewFile、カバー); }} catch(Exception ex){j2log(null、null、ex); }} / ** * MV Move File * * @Param Source * @Param Destination * @Param Cover * / public Static void MV(ファイルソース、ファイル宛先、ブールカバー){try {if(!source.exists()){throw 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( "_ copy"); } cp(source、new file(combain.getparent()、filename.tostring()))); } else {source.renameto(宛先); }} catch(Exception ex){j2log(null、null、ex); }} / **拡張ファイル拡張情報を取得 * * @param filepath * @param filename * @return * / private static string [] extensions(string filepath、string filename){string [] endix = {}; try {string fullpath = combinepath(filepath、filename);ファイルfile = new file(FullPath);拡張機能(ファイル); } catch(Exception ex){j2log(null、null、ex); } return拡張子; } / ** *拡張ファイル拡張情報を取得 * * @param fullpath * @return * / private static string [] extensions(string fullpath){string [] extension = {}; try {file file = new file(fullpath);拡張機能(ファイル); } catch(Exception ex){j2log(null、null、ex); } return拡張子; } / **拡張ファイル拡張情報を取得 * * @param file * @return * / private static string [] extensions(file file){string [] extension = {}; try {if(file.isfile()){string filename = file.getName(); if(filename.lastIndexof( " extension [0] = string.valueof(lastIndex); // "。"のインデックス " extension [1] = filename.substring(lastindex + 1); // endixed extension [2] = filename.substring(0、lastIndex); //ファイル名}}}}}}}}}} {j2log(null、null、ex); } return拡張子; } / ** * lsファイルを横断する * * @param filepath * @param loop * @return * / public static list <file> ls(string filepath、boolean loop){list <file> list = new ArrayList(); try {file file = new file(filepath); list.addall(ls(file、loop)); } catch(Exception ex){j2log(null、null、ex); }返品リスト。 } / ** * lsファイルを横断する * * @param file * @param loop * @return * / public static list <file> ls(file file、boolean loop){list <file> list = new ArrayList(); try {list.add(file); if(!file.isdirectory()){list.add(file); } 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); }返品リスト。 } / ** * filessort file sort(デフォルトの昇順注文) * * @param parentpath * @param sublist * @return * / private static file [] filessort(file [] infiles、boolean asc){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 [] fielaray = {}; if(files.size()> 0){filearray = list2Array(files); arrays.sort(feearray); 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()); }} concat2filearray(filearray、dirarray)を返します。 } / ** * concat2filearrayマージファイル配列 * * @param old1 * @param old2 * @return * / private static file [] concat2filearray(string [] old1、string [] old2){file [] new Array = newファイル[old1.length + old2.length]; for(int i = 0、n = old1.length; i <n; i ++){newArray [i] = new file(old1 [i]); } for(int i = 0、j = old1.length、n =(old1.length+old2.length); j <n; i ++、j ++){newArray [j] = new file(old2 [i]); } newArrayを返します。 } / ** *読み取りテキストファイル * * @param filepath * @param filename * @param charset * @return * / public static stringbuilder read(string filepath、string filename、string charset){stringbuilder sb = new StringBuilder(); try {string fullpath = combinepath(filepath、filename);ファイルfile = new file(FullPath); sb.append(filetools.tail(file、false、0、charset)); } catch(Exception ex){j2log(null、null、ex); } sbを返します。 } / ** *読み取りテキストファイル * * @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(file、false、0、charset)); } catch(Exception ex){j2log(null、null、ex); } sbを返します。 } / ** *読み取りテキストファイル * * @param file * @param charset * @return * / public static stringbuilder read(file file、string charset){stringbuilder sb = new StringBuilder(); try {sb.append(filetools.tail(file、false、0、charset)); } catch(Exception ex){j2log(null、null、ex); } sbを返します。 } / ** *テキストファイルの指定された行を読む * * * @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);ファイルfile = new file(FullPath); sb.append(filetools.tail(file、true、line、charset)); } catch(Exception ex){j2log(null、null、ex); } sbを返します。 } / ** *テキストファイルの指定された行を読む * * @param fullpath * @param line * @param charset * @return * / public static stringbuilder find(string fullpath、int line、string charset){stringbuilder sb = new StringBuilder(); try {file file = new file(fullpath); sb.append(filetools.tail(file、true、line、charset)); } catch(Exception ex){j2log(null、null、ex); } sbを返します。 } / ** *テキストファイルの指定された行を読む * * @param file * @param line * @param charset * @return * / public static stringbuilder find(fileファイル、int line、string charset){stringbuilder sb = new StringBuilder(); try {sb.append(filetools.tail(file、true、line、charset)); } catch(Exception ex){j2log(null、null、ex); } sbを返します。 } / ** * tailテキストファイルを読み取ります * * @param filepath * @param filename * @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);ファイルfile = new file(FullPath); sb.append(filetools.tail(file、find、line、charset)); } catch(Exception ex){j2log(null、null、ex); } sbを返します。 } / ** *テキスト読み取りテキストファイル * * @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(file、find、line、charset)); } catch(Exception ex){j2log(null、null、ex); } sbを返します。 } / ** *テキスト読み取りテキストファイル * * @paramファイル * @param charset * @param find * @param line * @return * / public static stringbuilder tail(ファイルファイル、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()){throw new filenotfoundecception(); }文字列fullpath = file.getPath(); BufferReader = new BufferedReader(new inputStreamReader(new FileInputStream(FullPath)、charset));弦の温度; for(int i = 0;(temp = bufferreader.readline())!= null; i ++){if(!find || line == i){sb.append(temp); }}} catch(Exception ex){j2log(null、null、ex); }最後に{if(null!= bufferReader){try {bufferreader.close(); } catch(ioException ex){j2log(null、null、ex); }}} sbを返します。 } / ** * sed読み取りテキストファイル * * @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);ファイルfile = new file(FullPath); list.addall(filetools.sed(file、charset)); } catch(Exception ex){j2log(null、null、ex); }返品リスト。 } / ** * sed読み取りテキストファイル * * @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(file、charset)); } catch(Exception ex){j2log(null、null、ex); }返品リスト。 } / ** * sed読み取りテキストファイル * * @param file * @param charset * @return * / public static list <string> sed(file file、string charset){list <string> list = new ArrayList(); BufferedReader BufferReader = null; if(null == charset || "" .equals(charset)){charset = "utf-8"; } try {if(!file.exists()|| file.isdirectory()){throw new filenotfoundecception(); }文字列fullpath = file.getPath(); BufferReader = new BufferedReader(new inputStreamReader(new FileInputStream(FullPath)、charset));弦の温度; for(int i = 0;(temp = bufferreader.readline())!= null; i ++){list.add(temp); }} catch(Exception ex){j2log(null、null、ex); }最後に{if(null!= bufferReader){try {bufferreader.close(); } catch(ioException ex){j2log(null、null、ex); }}} return list; } / ** * cat text text file * * @param filepath * @param filename * @return * / public static byte [] cat(string filepath、string filename){byte [] output = {}; try {string fullpath = combinepath(filepath、filename);ファイルfile = new file(FullPath); output = filetools.cat(file); } catch(Exception ex){j2log(null、null、ex); } return output; } / ** * cat読み取りテキストファイル * * @param fullpath * @return * / public static byte [] cat(string fullpath){byte [] output = {}; try {file file = new file(fullpath); output = filetools.cat(file); } catch(Exception ex){j2log(null、null、ex); } return output; } / ** * cat読み取りテキストファイル * * @param file * @return * / public static byte [] cat(file file){inputstream in = null; byte [] output = {}; try {if(!file.exists()|| file.isdirectory()){throw new filenotfoundexception(); }文字列fullpath = file.getPath(); long length = du(file、false);長い_2m = 2097152; byte [] bytes = new byte [(int)length]; in = new FileInputStream(FullPath); for(int count = 0; count!= -1;){if(length> 16 * _2m){length = 4 * _2m; } else if(length> 8 * _2m){length = 2 * _2m; } else if(length> 4 * _2m){length = _2m; } else if(length> 2 * _2m){length = _2m / 2; } else if(length> _2m){length = _2m / 4; } else {length = 4096; } bytes = new byte [(int)length]; count = in.read(bytes);出力= concatarray(バイト、出力); length = in.abailable(); }} catch(Exception ex){j2log(null、null、ex); }最後に{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){byarte [] newArray = new byte [old1.length + old2.length]; System.ArrayCopy(Old1、0、NewArray、0、Old1.Length); System.ArrayCopy(Old2、0、NewArray、Old1.Length、Old2.Length); NewArrayを返します。 } / ** *書き込みファイルフルパスコンテンツコンテンツ * * @param filepath * @param filename * @param content * @param content * / public static void dd(string filepath、string filename、byte [] content、boolean isappend){try {string fullpath = combinepath、filepath、filename);ファイルfile = new file(FullPath); filetools.dd(file、content、isAppend); } catch(Exception ex){j2log(null、null、ex); }} / ** * dd書き込みファイルフルパスコンテンツコンテンツ * * @param fullpath * @param content * @param isappend * / public static void dd(string fullpath、byte [] content、boolean isappend){try {file file = new file(fullpath); filetools.dd(file、content、isAppend); } catch(Exception ex){j2log(null、null、ex); }} / ** * dd書き込みファイルフルパスコンテンツコンテンツ * * @param file * @param content * @param isappend * / public static void dd(file file、byte [] content、boolean isappend){fileoutputStream fileoutputStream = null; try {if(!file.exists()){file.createNewFile(); } fileoutputStream = new fileoutputStream(file、isAppend); fileoutputStream.write(content); } catch(Exception ex){j2log(null、null、ex); }最後に{try {if(null!= fileoutputStream){fileoutputStream.close(); }} catch(ioException ex){j2log(null、null、ex); }}}} / ** *書き込みファイルコンテンツをファイルFullPath * * @param filepath * @param filename * @param content * / public static void write(string filepath、string filename、string content){try {string fullpath = combine(filepath、filename);ファイルfile = new file(FullPath); filetools.write(file、content、true); } catch(Exception ex){j2log(null、null、ex); }} / ** *書き込みファイルコンテンツをファイルFULLPATH * * @PARAM FULLPATH * @Param Content * / public static void write(string fullpath、string content){try {file file = new file(fullpath); filetools.write(file、content、true); } catch(Exception ex){j2log(null、null、ex); }} / ** *書き込みファイルコンテンツをファイルFULLPATH * * @PARAMファイル * @Param Content * / public Static void write(file file、string content){try {filetools.write(file、content、true); } catch(Exception ex){j2log(null、null、ex); }} / ** write write write(append)ファイルコンテンツをファイルFullpath * * @param filepath * @param filename * @param content * @param content * / public static void write(string filepath、string filename、string content、boolean isappend){try {string fullpath = combinepath、filename、filename);ファイルfile = new file(FullPath); filetools.write(file、content、isAppend); } catch(Exception ex){j2log(null、null、ex); }} / ** write write(append)ファイルコンテンツをファイル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(file、content、isAppend); } catch(Exception ex){j2log(null、null、ex); }} / ** write write write(append)ファイルコンテンツをファイルFullpath * * @param content * @param isappend * / public static void write(file file、string content、boolean isappend){filewriter filewriter = null; try {if(!file.exists()){file.createNewFile(); } filewriter = new filewriter(file.getPath()、isAppend); filewriter.write(content); } catch(Exception ex){j2log(null、null、ex); }最後に{if(null!= filewriter){try {filewriter.close(); } catch(ioException ex){j2log(null、null、ex); }}}}} / ** *テールファイルコンテンツをファイルのインデックスに追加する場所 * * @param filepath * @param filename * @param content * @param index * / public static void tael(string filename、string filename、string filename、string filename、string filename、long index){try {string fullpath = combine path(filepath、filename);ファイルfile = new file(FullPath); filetools.tail(file、content、index); } catch(Exception ex){j2log(null、null、ex); }} / ** *テールファイルのコンテンツをファイルのインデックスに追加する * * @param fullpath * @param content * @param index * / public static void tail(string fullpath、string content、long index){try {file file = new file(fullpath); filetools.tail(file、content、index); } catch(Exception ex){j2log(null、null、ex); }} / ** *テールファイルのコンテンツをファイルのインデックスに追加する * * @param file * @param content * @param index * / public static void tail(fileファイル、文字列コンテンツ、long index){randomacessfile randomaccessfile = null; try {if(!file.exists()){file.createNewFile(); } randomAccessFile = new RandomAccessFile(file.getPath()、 "rw"); randomaccessfile.seek(index); randomAccessfile.writeBytes(content); } catch(Exception ex){j2log(null、null、ex); }最後に{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 = new file(FullPath); file = mkdir(file); } catch(Exception ex){j2log(null、null、ex); }ファイルを返します。 } / ** * mkdir create directory * * @param fullpath * @return * / public static file mkdir(string fullpath){file file = null; try {file = new file(FullPath); file = mkdir(file); } catch(Exception ex){j2log(null、null、ex); }ファイルを返します。 }/** * mkdir create directory * * @param file * @return */public static file mkdir(file file){try {if(!file.Exists()){file.mkdir(); }ファイルを返します。 } / ** touch Create file * * @param filepath * @param filename * / public static void touch(string filepath、string filename){try {string fullpath = combinepath(filepath、filename);ファイルfile = new file(FullPath); touch(file); } catch(Exception ex){j2log(null、null、ex); }} / **タッチファイルのタッチ * * @param fullpath * / public static void touch(string fullpath){try {file file = new file(fullpath); touch(file); } catch(Exception ex){j2log(null、null、ex); }}/** touch Create file * * @param file */public static void touch(file file){try {if(!file.exists()){file.createNewfile(); //ファイルが存在しない場合}}} catch(exception ex){j2log(null、null、ex); }} / ** * rmファイルを削除 * * @param filepath * @param filename * / public static void rm(string filepath、string filename){try {string fullpath = combinepath(filepath、filename);ファイルfile = new file(FullPath); rm(file); } 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(file); } catch(Exception ex){j2log(null、null、ex); }} / ** * rm delete file * * @param file * / public static void rm(file file){try {if(!file.exists()){throw new filenotfoundexception(); } if(file.isfile()){file.delete(); }} catch(Exception ex){j2log(null、null、ex); }} / ** * rmdir Delete Directory * * @param filepath * @param filename * @param loop * / public static void rmdir(string filepath、string filename、boolean loop){try {string fullpath = combine(filepath、filename);ファイルdir = new File(FullPath); rmdir(dir、loop); } catch(Exception ex){j2log(null、null、ex); }} / ** * rmdir Delete Directory * * @Param FullPath * @Param Loop * / public static void rmdir(String FullPath、Boolean Loop){try {file dir = new file(FullPath); rmdir(dir、loop); } catch(Exception ex){j2log(null、null、ex); }} / ** * rmdir Delete Directory * * @param dir * @param loop * / public static void rmdir(file dir、boolean loop){try {if(!dir.exists()){throw new filenotfoundexception(); } if(dir.isdirectory()){file [] files = dir.listfiles(); int length = files.length; for(int i = 0; i <length && loop; i ++){if(files [i] .isdirectory()){rmdir(files [i]、loop); } else {rm(files [i]); }} if(loop || length == 0){dir.delete(); }}} catch(Exception ex){j2log(null、null、ex); }} / ** * du実際のファイルサイズを取得 * * @param filepath * @param filename * @param loop * @return * / public static long du(string filepath、string filename、boolean loop){long size = 0; try {string fullpath = combinepath(filepath、filename);ファイルfile = new file(FullPath); size = du(file、loop); } catch(Exception ex){j2log(null、null、ex); } return size; } / ** * du実際のファイルサイズを取得 * * @param filepath * @param filename * @return * / public static long du(string filepath、string filename){long size = 0; try {string fullpath = combinepath(filepath、filename);ファイルfile = new file(FullPath); size = du(file、false); } catch(Exception ex){j2log(null、null、ex); } return size; } / ** * du実際のファイルサイズを取得 * * @param fullpath * @return * / public static long du(string fullpath){long size = 0; try {file file = new file(fullpath); size = du(file、false); } catch(Exception ex){j2log(null、null、ex); } return size; } / ** * du実際のファイルサイズを取得 * * @param file * @return * / public static long du(file file){long size = 0; try {size = du(file、false); } catch(Exception ex){j2log(null、null、ex); } return size; } / ** * du実際のファイルサイズを取得 * * @param fullpath * @param loop * @return * / public static long du(string fullpath、boolean loop){long size = 0; try {file file = new file(fullpath); size = du(file、loop); } catch(Exception ex){j2log(null、null、ex); } return size; } / ** * du実際のファイルサイズを取得 * * @param file * @param loop * @return * / public static long du(file file、boolean loop){filechannel filechannel = null; long size = 0; try {if(!file.exists()){new 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; for(int i = 0; i <length && loop; i ++){if(files [i] .isdirectory()){du(files [i]、loop); } else {size += du(files [i]、false); }}}} catch(Exception ex){j2log(null、null、ex); }最後に{if(null!= filechannel){try {filechannel.close(); } catch(Exception ex){j2log(null、null、ex); }}} return size; }}上記はこの記事に関するものです。すべての人の学習に役立つことを願っています。