Java I/O流
2016-04-04 11:46
453 查看
Java I/O流
用文件通道(FileChannel)来实现文件快速复制程序运行时间截图
FileChannel方式截图缓冲输入输出流方式截图
程序代码
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class copyFileChannel { public static void copy(File a, File temp) { FileInputStream fi = null; FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(a); fo = new FileOutputStream(temp); in = fi.getChannel();// 得到对应文件通道 out = fo.getChannel();// 得到对应文件通道 in.transferTo(0, in.size(), out);// 连接两个通道 } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); fo.close(); in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { // TODO Auto-generated method stub File a = new File("a.mp3"); File temp = new File("temp.mp3"); long start, end; start = System.currentTimeMillis(); copy(a, temp); end = System.currentTimeMillis(); System.out.println("复制,用时" + (end - start) + "ms"); } }
相关文章推荐
- java重构、重载、重写
- java文件io之PrintWriter
- spring框架中多数据源创建加载并且实现动态切换的配置实例代码
- [Java开发之路](25)引用类型
- [Java开发之路](24)内部类
- java中多态的详解
- java基础之synchronized使用方法
- java多线程--定时器Timer的使用
- Java Map用法
- myeclipse和svn通过代理方式更新代码
- 共同学习Java源码--常用数据类型--String(八)
- spring笔记
- spring整合和事物策略
- JAVA如何将ip字符串转化为整型的工具方法
- Java数据库连接——JDBC调用存储过程,事务管理和高级应用
- eclipseGUI的可视化开发工具插件
- Java中的ReentrantLock和synchronized两种锁定机制的对比
- 关于eclipse或Android studio中adb连接上不了手机的解决方法
- eclipse web开发插件安装
- JAVA 对象引用,以及对象赋值