Javaを使用してファイルをコピーすることは、どこでも使用できます。これが参照のためのクラスです。合計2つの方法があります。
public static boolean copyfile(string srcfilename、string destfilename、boolean overlay); public static boolean copydirctory(string srcdirname、string destdirname、boolean overlay);
で:
コピーするsrcfileNameファイル名
descfileNameターゲットファイル名
オーバーレイターゲットファイルが存在する場合、それを上書きするかどうか。コピーが成功した場合、trueを返し、それ以外の場合はfalseを返します
コード:
java.io.fileをインポートします。 java.io.fileinputStreamをインポートします。 java.io.filenotfoundexceptionをインポートします。 java.io.fileoutputStreamをインポートします。 java.io.ioexceptionをインポートします。 java.io.inputStreamをインポートします。 java.io.outputStreamをインポートします。 javax.swing.joptionpaneをインポートします。 / ** *ファイルまたはフォルダーをコピー * * zww */ public class copyfileutil {private static string message = ""; / ** *単一のファイルをコピー * * @param srcfilename *ファイル名をコピーする * @param descfilename *ターゲットファイル名 * @param overlay *ターゲットファイルが存在する場合、コピーが成功した場合、@returnを上げるかどうか、それ以外の場合はfalse */ public static boolean copyfile(srcfilename、srcfileaname、srcfileaname、srcfileaname、booleaname) srcfile = new file(srcfilename); //ソースファイルが存在するかどうかを決定する場合(!srcfile.exists()){message = "source file:" + srcfilename + "not intect!"; joptionpane.showmessagedialog(null、message); falseを返します。 } else if(!srcfile.isfile()){message = "copy file failed、source file:" + srcfilename + "not a file!"; joptionpane.showmessagedialog(null、message); falseを返します。 } //ターゲットファイルがファイルdestfile = newファイル(DestFileName)が存在するかどうかを決定します。 if(destfile.exists()){//ターゲットファイルが存在し、ターゲットファイルがディレクトリであるか単一のファイルであるかどうかに関係なく、既存のターゲットファイルを削除する場合、上書きを許可している場合、既存のターゲットファイルを削除します。 }} else {//ターゲットファイルが存在しないディレクトリの場合、ディレクトリを作成する場合、(!destfile.getParentFile()。exists()){//ターゲットファイルが存在しない場合(!destfile.getParentFile()。mkdirs()){//ターゲットファイルのコピーに失敗した場合、ファイルはfileに失敗しました。 }}} // file int byteread = 0をコピーします。 //バイト数inputstream in = null; outputStream out = null; try {in = new FileInputStream(srcfile); out = new fileoutputStream(destfile); byte [] buffer = new byte [1024]; while((byteread = in.read(buffer))!= -1){out.write(buffer、0、byteread); } trueを返します。 } catch(filenotfoundexception e){return false; } catch(ioException e){false; }最後に{try {if(out!= null)out.close(); if(in!= null)in.close(); } catch(ioexception e){e.printstacktrace(); }}}} / ** *ディレクトリ全体のコンテンツをコピー * * * @param srcdirname *ディレクトリのディレクトリ名コピー * @param destdirname * destingディレクトリ名 * @param overlay *ターゲットディレクトリが存在する場合 * destdirname、boolean overlay){//ソースディレクトリが存在するかどうかを決定するファイルsrcdir = new file(srcdirname); if(!srcdir.exists()){message = "copy directory failed:source directory" + srcdirname + "do n't stabe!"; joptionpane.showmessagedialog(null、message); falseを返します。 } else if(!srcdir.isdirectory()){message = "copy directory failed:" + srcdirname + "not a directory!"; joptionpane.showmessagedialog(null、message); falseを返します。 } //ターゲットディレクトリ名がファイル区切り文字で終了しない場合、ファイル区分を追加しますif(!destdirname.endswith(file.separator)){destdirname = destdirname + file.separator; } file destdir = new file(destdirname); //ターゲットフォルダーが存在する場合(destdir.exists()){//オーバーレイが許可されている場合、既存のターゲットディレクトリを削除します(overlay){new file(destdirname).delete(); } else {message = "コピーディレクトリに失敗:宛先ディレクトリ" + destdirname + "既に存在する!"; joptionpane.showmessagedialog(null、message); falseを返します。 }} else {//宛先ディレクトリシステムを作成します。 if(!destdir.mkdir()){system.out.println( "コピーディレクトリに失敗:宛先ディレクトリの作成に失敗!"); falseを返します。 }} boolean flag = true; file [] files = srcdir.listfiles(); for(int i = 0; i <files.length; i ++){// file if(files [i] .isfile()){flag = copyfileutil.copyfile(files [i] .getabsolutepath()、destdirname+files [i] .getname()、overlay); if(!flag)break; } else if(files [i] .isdirectory()){flag = copyfileutil.copydirectory(files [i] .getabsolutepath()、destdirname + files [i] .getname()、overlay); if(!flag)break; }} if(!flag){message = "copy directory" + srcdirname + "to" + destdirname + "failed!"; joptionpane.showmessagedialog(null、message); falseを返します。 } else {return true; }} public static void main(string [] args){string srcdirname = "c:/test/test0/test1";文字列destdirname = "c:/ttt"; copyfileutil.copydirctory(srcdirname、destdirname、true); }}マルチスレッドの最適化を考慮せずに、単一スレッドファイルをコピーする最速の方法は(ファイルが大きいほど、一般的に使用されるメソッドよりも30+%+30+%高速です):
private static void niotransfercopy(ファイルソース、ファイルターゲット){filechannel in = null; filechannel out = null; fileInputStream instream = null; fileoutputStream OutStream = null; try {instream = new fileinputStream(source); outStream = new fileoutputStream(ターゲット); in = instream.getChannel(); out = outstream.getChannel(); in.transferto(0、in.size()、out); } catch(ioexception e){e.printstacktrace(); }最後に{close(instream);閉じる(in);閉じる(外側);閉じる(out); }}複製の進行状況を監視する必要がある場合は、2番目に速い方法を使用できます(速度に大きな影響を与えるバッファのサイズに注意を払ってください):
private static void niobuffercopy(ファイルソース、ファイルターゲット){filechannel in = null; filechannel out = null; fileInputStream instream = null; fileoutputStream OutStream = null; try {instream = new fileinputStream(source); outStream = new fileoutputStream(ターゲット); in = instream.getChannel(); out = outstream.getChannel(); bytebuffer buffer = bytebuffer.allocate(4096); while(in.read(buffer)!= -1){buffer.flip(); out.write(buffer); buffer.clear(); }} catch(ioexception e){e.printstacktrace(); }最後に{close(instream);閉じる(in);閉じる(外側);閉じる(out); }}一般的に使用される方法1は次のとおりです。
private static void custombufferbufferedstreamcopy(ファイルソース、ファイルターゲット){inputstream fis = null; outputStream fos = null; try {fis = new BufferedInputStream(new FileInputStream(source)); fos = new BufferedOutputStream(new FileOutputStream(ターゲット)); byte [] buf = new byte [4096]; int i; while((i = fis.read(buf))!= -1){fos.write(buf、0、i); }} catch(例外e){e.printstacktrace(); }最後に{close(fis); close(fos); }}一般的に使用される方法2は次のとおりです。
private static void custombufferstreamcopy(ファイルソース、ファイルターゲット){inputstream fis = null; outputStream fos = null; try {fis = new fileinputStream(source); fos = new fileoutputStream(ターゲット); byte [] buf = new byte [4096]; int i; while((i = fis.read(buf))!= -1){fos.write(buf、0、i); }} catch(例外e){e.printstacktrace(); }最後に{close(fis); close(fos); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。