文件传输的几种方式效率比较 博客分类: IO流 ioniochannletransferrenameTo
程序员文章站
2024-03-11 23:43:19
...
package com.yonge.nio; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; /** * 文件传输几种方式比较 * @author wb-gaoy * @version $Id: ChannelTest.java,v 0.1 2012-12-13 下午7:28:40 wb-gaoy Exp $ */ public class ChannelTest { public boolean copyFileByIO(File srcFile, File targetDir) throws IOException { FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(targetDir + File.separator + srcFile.getName()); byte[] bytes = new byte[1024 * 1024]; int length = -1; while ((length = fis.read(bytes)) != -1) { fos.write(bytes, 0, length); } fis.close(); fos.close(); return true; } public boolean copyFileByNIO(File srcFile, File targetDir) throws IOException { FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(targetDir + File.separator + srcFile.getName()); try { FileChannel fisChannel = fis.getChannel(); FileChannel fosChannel = fos.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024); while (fisChannel.read(byteBuffer) != -1) { byteBuffer.flip(); fosChannel.write(byteBuffer); byteBuffer.clear(); } fosChannel.close(); fisChannel.close(); } finally { fis.close(); fos.close(); } return true; } public boolean copyFileByTransfer(File srcFile, File targetDir) throws IOException { FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(targetDir + File.separator + srcFile.getName()); try { FileChannel fisChannel = fis.getChannel(); FileChannel fosChannel = fos.getChannel(); fisChannel.transferTo(0, fisChannel.size(), fosChannel); fosChannel.close(); fisChannel.close(); } finally { fis.close(); fos.close(); } return true; } //注意:如果目标文件已经存在或磁盘间的格式不一样(例如源文件磁盘是ntfs,而目标磁盘是fat32),则不会成功 public boolean moveFile(File srcFile, File targetFile) { srcFile.renameTo(new File(targetFile + File.separator + srcFile.getName())); return true; } /** * @param args */ public static void main(String[] args) { final ChannelTest test = new ChannelTest(); final File targetDir = new File("e:/temp"); Thread thread1 = new Thread(new Runnable() { @Override public void run() { try { long start = System.currentTimeMillis(); test.copyFileByIO(new File("e:/ppl20120921145445000611.rar"), targetDir); System.out.println("IO cost:" + (System.currentTimeMillis() - start) + "ms"); } catch (IOException e) { e.printStackTrace(); } } }, "A"); thread1.start(); Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { long start = System.currentTimeMillis(); test.copyFileByNIO(new File("e:/ppl20120921145445000612.rar"), targetDir); System.out.println("NIO cost:" + (System.currentTimeMillis() - start) + "ms"); } catch (IOException e) { e.printStackTrace(); } } }, "B"); thread2.start(); Thread thread3 = new Thread(new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); test.moveFile(new File("e:/ppl20120921145445000613.rar"), targetDir); System.out.println("Move cost:" + (System.currentTimeMillis() - start) + "ms"); } }, "C"); thread3.start(); Thread thread4 = new Thread(new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); try { test.copyFileByTransfer(new File("e:/ppl20120921145445000614.rar"), targetDir); } catch (IOException e) { e.printStackTrace(); } System.out.println("Transfer cost:" + (System.currentTimeMillis() - start) + "ms"); } }, "D"); thread4.start(); } }
上面是四种文件传输方式,测试结果是transfer的效率最低,而网上都说这种效率高,不知道是什么原因,欢迎大家测试一下,说说各自的想法。