使用NIO進行文件拷貝
只要三行代碼進行文件拷貝,嘿嘿,當然三行中不包含文件是否存在的判斷和異常的處理了,只是想說明一下,采用FileChannel的API的方便性。
import java.io.FileInputStream ; import java.io.FileOutputStream ; import java.io.IOException ; import java.nio.channels.FileChannel ;public class FileCopy { public static void main(String[]args) throws IOException{ String sourcefile="E:\參考資料\設計模式.pdf"; String targetfile = "E:\參考資料\設計模式1.pdf"; copyfile(sourcefile, targetfile); } /**
* * 方法用途:文件拷貝 * 方法名:copyfile * 返回值:void * * 參數:@param sourcefile 源文件 * 參數:@param targetfile 目標文件 * 參數:@throws IOException */ private static void copyfile(String sourcefile,String targetfile) throws IOException{ FileChannel sourcefc = new FileInputStream(sourcefile).getChannel(); FileChannel targetfc = new FileOutputStream(targetfile).getChannel(); sourcefc.transferTo(0,sourcefc.size(),targetfc); //上面沒有進行文件是否存在的判斷和異常的處理 }
}</pre>
本文由用戶 mxd2 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!