這篇文章給大家分享的是有關(guān)如何通過JAVA NIO通道傳輸拷貝文件的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
成都創(chuàng)新互聯(lián)公司是專業(yè)的西吉網(wǎng)站建設(shè)公司,西吉接單;提供成都做網(wǎng)站、成都網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行西吉網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
通過JAVA NIO 通道傳輸拷貝文件
方式一
/** * 通過JAVA NIO 通道傳輸拷貝文件 * * @param sourcePath 源文件路徑 * @param targetPath 目標(biāo)文件路徑 */ public static void copyFileByChannelTransfer(String sourcePath, String targetPath) { FileChannel inChannel = null; FileChannel outChannel = null; try { //獲取通道 inChannel = FileChannel.open(Paths.get(sourcePath), StandardOpenOption.READ); outChannel = FileChannel.open(Paths.get(targetPath),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE); inChannel.transferTo(0,inChannel.size(),outChannel); } catch (IOException e) { e.printStackTrace(); }finally { //關(guān)閉流 try { if (outChannel != null) { outChannel.close(); } if (inChannel != null) { inChannel.close(); } } catch (IOException e) { e.printStackTrace(); } } }
方式二
/** * 通過JAVA NIO 通道傳輸拷貝文件 * * @param sourcePath 源文件路徑 * @param targetPath 目標(biāo)文件路徑 */ public static void copyFileByChannelTransfer2(String sourcePath, String targetPath) { FileInputStream fis = null; FileOutputStream fos = null; FileChannel inChannel = null; FileChannel outChannel = null; try { fis = new FileInputStream(sourcePath); fos = new FileOutputStream(targetPath); //獲取通道 inChannel = fis.getChannel(); outChannel = fos.getChannel(); inChannel.transferTo(0,inChannel.size(),outChannel); } catch (IOException e) { e.printStackTrace(); }finally { //關(guān)閉流 try { if (outChannel != null) { outChannel.close(); } if (inChannel != null) { inChannel.close(); } } catch (IOException e) { e.printStackTrace(); } } }
使用示例
String source = "e:\\demo\\縱天神帝.txt"; String target = "e:\\demo\\"; long time1 = System.currentTimeMillis(); copyFileByStream(source, target + "1.txt"); System.out.println("通過字節(jié)流實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time1)); long time2 = System.currentTimeMillis(); copyFileByReaderAndWriter(source, target + "2.txt"); System.out.println("通過字符流實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time2)); long time3 = System.currentTimeMillis(); copyFileByBuffered(source, target + "3.txt"); System.out.println("通過字節(jié)緩沖流實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time3)); long time4 = System.currentTimeMillis(); copyFileByBufferedChar(source, target + "4.txt"); System.out.println("通過字符緩沖流實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time4)); long time5 = System.currentTimeMillis(); copyFileByChannel(source, target + "5.txt"); System.out.println("通過JAVA NIO通道(非直接緩沖區(qū))實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time5)); long time6 = System.currentTimeMillis(); copyFileByChannelBufferd(source, target + "6.txt"); System.out.println("通過JAVA NIO通道(直接緩沖區(qū))實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time6)); long time7 = System.currentTimeMillis(); copyFileByChannelTransfer(source, target + "7.txt"); System.out.println("通過JAVA NIO通道傳輸實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time7)); long time8 = System.currentTimeMillis(); copyFileByChannelTransfer(source, target + "8.txt"); System.out.println("通過JAVA NIO通道傳輸2實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time8));
感謝各位的閱讀!關(guān)于“如何通過JAVA NIO通道傳輸拷貝文件”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
網(wǎng)站題目:如何通過JAVANIO通道傳輸拷貝文件
當(dāng)前鏈接:http://aaarwkj.com/article26/igsecg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)站營銷、搜索引擎優(yōu)化、企業(yè)建站、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)