您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關如何通過JAVA NIO通道傳輸拷貝文件的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
通過JAVA NIO 通道傳輸拷貝文件
方式一
/** * 通過JAVA NIO 通道傳輸拷貝文件 * * @param sourcePath 源文件路徑 * @param targetPath 目標文件路徑 */ 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 { //關閉流 try { if (outChannel != null) { outChannel.close(); } if (inChannel != null) { inChannel.close(); } } catch (IOException e) { e.printStackTrace(); } } }
方式二
/** * 通過JAVA NIO 通道傳輸拷貝文件 * * @param sourcePath 源文件路徑 * @param targetPath 目標文件路徑 */ 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 { //關閉流 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("通過字節流實現文件的拷貝耗時:" + (System.currentTimeMillis() - time1)); long time2 = System.currentTimeMillis(); copyFileByReaderAndWriter(source, target + "2.txt"); System.out.println("通過字符流實現文件的拷貝耗時:" + (System.currentTimeMillis() - time2)); long time3 = System.currentTimeMillis(); copyFileByBuffered(source, target + "3.txt"); System.out.println("通過字節緩沖流實現文件的拷貝耗時:" + (System.currentTimeMillis() - time3)); long time4 = System.currentTimeMillis(); copyFileByBufferedChar(source, target + "4.txt"); System.out.println("通過字符緩沖流實現文件的拷貝耗時:" + (System.currentTimeMillis() - time4)); long time5 = System.currentTimeMillis(); copyFileByChannel(source, target + "5.txt"); System.out.println("通過JAVA NIO通道(非直接緩沖區)實現文件的拷貝耗時:" + (System.currentTimeMillis() - time5)); long time6 = System.currentTimeMillis(); copyFileByChannelBufferd(source, target + "6.txt"); System.out.println("通過JAVA NIO通道(直接緩沖區)實現文件的拷貝耗時:" + (System.currentTimeMillis() - time6)); long time7 = System.currentTimeMillis(); copyFileByChannelTransfer(source, target + "7.txt"); System.out.println("通過JAVA NIO通道傳輸實現文件的拷貝耗時:" + (System.currentTimeMillis() - time7)); long time8 = System.currentTimeMillis(); copyFileByChannelTransfer(source, target + "8.txt"); System.out.println("通過JAVA NIO通道傳輸2實現文件的拷貝耗時:" + (System.currentTimeMillis() - time8));
感謝各位的閱讀!關于“如何通過JAVA NIO通道傳輸拷貝文件”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。