文件复制.md 发表于 2020-08-10 更新于 2021-11-21 分类于 网络编程 本文字数: 3.9k 阅读时长 ≈ 4 分钟 四种复制文件的方法 字节复制 缓冲流复制 使用Buffer的FileChanel 直接使用FileChannel 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135/** * @作者 ming * @目标 当你的才华还撑不起你的野心时,就应该静下心来学习; * 当你的能力还驾驭不了你的目标时,就应该沉下心来历练; * 梦想不是浮躁,而是沉淀和积累。 * @创建时间 2020/8/9 14:29 */package filecopy;import java.io.*;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;public class FileCopyTest { public static void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { FileCopyRunner runner1 = (source, target) -> { InputStream fin = null; OutputStream fout = null; try { fin = new FileInputStream(source); fout = new FileOutputStream(target); int size = 0; while ((size = fin.read()) != -1) { fout.write(size); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { close(fin); close(fout); } }; long startTime = System.currentTimeMillis(); File sourceFile1 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\test.doc"); File targetFile1 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\runner1.doc"); runner1.copyFile(sourceFile1, targetFile1); System.out.println("runner1总耗时:" + (System.currentTimeMillis() - startTime)); FileCopyRunner runner2 = (source, target) -> { InputStream fin = null; OutputStream fout = null; try { fin = new BufferedInputStream(new FileInputStream(source)); fout = new BufferedOutputStream(new FileOutputStream(target)); byte[] buffer = new byte[1024]; while ((fin.read(buffer)) != -1) { fout.write(buffer, 0, buffer.length); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { close(fin); close(fout); } }; startTime = System.currentTimeMillis(); File sourceFile2 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\test.doc"); File targetFile2 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\runner2.doc"); runner1.copyFile(sourceFile2, targetFile2); System.out.println("runner2总耗时:" + (System.currentTimeMillis() - startTime)); FileCopyRunner runner3 = (source, target) -> { FileChannel fin = null; FileChannel fout = null; try { fin = new FileInputStream(source).getChannel(); fout = new FileOutputStream(target).getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while (fin.read(buffer) != -1) { // 切换为写模式 buffer.flip(); while (buffer.hasRemaining()) { fout.write(buffer); } // 切换为读模式 buffer.clear(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { close(fin); close(fout); } }; startTime = System.currentTimeMillis(); File sourceFile3 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\test.doc"); File targetFile3 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\runner3.doc"); runner1.copyFile(sourceFile3, targetFile3); System.out.println("runner3总耗时:" + (System.currentTimeMillis() - startTime)); FileCopyRunner runner4 = (source, target) -> { FileChannel fin = null; FileChannel fout = null; try { fin = new FileInputStream(source).getChannel(); fout = new FileOutputStream(target).getChannel(); long totalSize = 0; while (totalSize != fin.size()) { totalSize += fin.transferTo(0, fin.size(), fout); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { close(fin); close(fout); } }; startTime = System.currentTimeMillis(); File sourceFile4 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\test.doc"); File targetFile4 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\runner4.doc"); runner1.copyFile(sourceFile4, targetFile4); System.out.println("runner4总耗时:" + (System.currentTimeMillis() - startTime)); }} 一毛也是爱~ 打赏 微信支付