通过对上面两个方法测试,我们可以知道使用文件通道的方式复制文件,明显比输出流复制文件效率要高。学到的知识点:
①:返回系统的当前时间:start = System.currentTimeMillis();
②:write(byte[] b, int off, int len) 方法
将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流
b - 数据off - 数据中的起始偏移量。len - 要写入的字节数。
③:in.read(buffer))!=-1,是从流buffer中读取一个字节,当流结束的时候read返回-1
④:in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道中
public abstract long transferTo(long position, long count,
WritableByteChannel target)throws IOException;
position - 文件中的位置,从此位置开始传输;必须为非负数
count - 要传输的最大字节数;必须为非负数
target - 目标通道
返回:
实际已传输的字节数,可能为零
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |