Java NIO复制文件功能
来源:互联网 发布:视频采集源码 编辑:程序博客网 时间:2024/06/12 01:20
1. package com.ghb.crp.file; 2. 3. import java.io.BufferedInputStream; 4. import java.io.BufferedOutputStream; 5. import java.io.File; 6. import java.io.FileInputStream; 7. import java.io.FileOutputStream; 8. import java.nio.ByteBuffer; 9. import java.nio.channels.FileChannel; 10. 11. public class FileCopy { 12. public static String src = "f://ss.rmvb"; 13. public static String dist = "f://ss2.rmvb"; 14. 15. /** 16. * @param args 17. */ 18. public static void main(String[] args) { 19. long start = System.currentTimeMillis(); 20. copyNew(); 21. long end = System.currentTimeMillis(); 22. System.out.println("nio 用时: " + (end - start)); 23. 24. long start2 = System.currentTimeMillis(); 25. copyOld(); 26. end = System.currentTimeMillis(); 27. System.out.println("io 用时: " + (end - start2)); 28. } 29. 30. public static void copyOld() { 31. File srcFile = new File(src); 32. File distFile = new File(dist); 33. if (distFile.exists()) { 34. distFile.delete(); 35. } 36. try { 37. FileInputStream fin = new FileInputStream(srcFile); 38. BufferedInputStream buffIn = new BufferedInputStream(fin); 39. 40. FileOutputStream fout = new FileOutputStream(distFile); 41. BufferedOutputStream buffout = new BufferedOutputStream(fout); 42. byte[] bytes = new byte[1024]; 43. while (buffIn.read(bytes) > 0) { 44. buffout.write(bytes); 45. } 46. fin.close(); 47. buffIn.close(); 48. fout.close(); 49. buffout.close(); 50. } catch (Exception e) { 51. e.printStackTrace(); 52. } 53. 54. } 55. 56. public static void copyNew() { 57. try { 58. File srcFile = new File(src); 59. File distFile = new File(dist); 60. if (distFile.exists()) { 61. distFile.delete(); 62. } 63. 64. FileInputStream fin = new FileInputStream(srcFile); 65. FileOutputStream fout = new FileOutputStream(distFile); 66. FileChannel inChannel = fin.getChannel(); 67. FileChannel outChannel = fout.getChannel(); 68. int ByteBufferSize = 1024 * 100; 69. ByteBuffer buff = ByteBuffer.allocate(ByteBufferSize); 70. 71. while (inChannel.read(buff) > 0) { 72. buff.flip(); 73. if (inChannel.position() == inChannel.size()) {// 判断是不是最后一段数据 74. int lastRead = (int) (inChannel.size() % ByteBufferSize); 75. byte[] bytes = new byte[lastRead]; 76. buff.get(bytes, 0, lastRead); 77. outChannel.write(ByteBuffer.wrap(bytes)); 78. buff.clear(); 79. } else { 80. outChannel.write(buff); 81. buff.clear(); 82. } 83. }// 这个使用FileChannel 自带的复制 84. // outChannel.transferFrom(inChannel, 0, inChannel.size()); 85. outChannel.close(); 86. inChannel.close(); 87. fin.close(); 88. fout.close(); 89. } catch (Exception e) { 90. e.printStackTrace(); 91. } 92. } 93. }
0 0
- java nio 复制文件功能
- Java NIO复制文件功能
- java NIO复制文件
- JAVA功能代码《12》----使用NIO快速复制文件
- 【Java IO】NIO复制文件
- java文件普通复制和NIO复制
- JAVA IO和NIO复制文件
- java实现文件复制功能
- java实现文件复制功能
- 利用Java NIO 实现文件监控功能。
- 使用NIO复制文件
- NIO 复制文件。
- NIO复制文件
- Java NIO文件复制以及与传统IO效率对比
- java NIO实现的文件复制(包括文件与文件夹复制) NIO读取数据与写数据
- JAVA 实现文件的“复制”功能
- 用java实现文件的复制功能
- java io 复制文件与 nio 复制文件的效率问题
- 数据结构颓废计划VI-堆
- 10007---临时急需马上说02
- Python的lambda函数与排序
- 作为测试人员,我是这么报BUG的
- MVC 模型 实践
- Java NIO复制文件功能
- Ubuntu环境下制作win7-U盘引导盘
- 完美解决ajax跨域请求的问题
- Python爬取成绩
- 写在生日当天
- 数据持久层DAO的建立和使用,实现数据库的连接
- Python Itertools
- 大数据下多线程对文件的检索
- Linux 下的一些常用命令