不懂就问, Java 中文件复制速度的问题。
資深大佬 : zhao1014 5
代码如下:
public class copy { public static void main(String[] args) throws IOException { long s = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("C:\Users\ZZY\Desktop\JavaNote\02-Java 语言进阶\原理图\哈希表.png"); FileOutputStream fos = new FileOutputStream("C:\Users\ZZY\Desktop\哈希表.png"); byte[] bytes = new byte[1024]; int len = 0; while((len = fis.read()) != -1){ fos.write(bytes,0,len); } fos.close(); fis.close(); long e = System.currentTimeMillis(); System.out.println("文件输出耗时" + (e-s) + "毫秒" ); }
}
字节数组长度设置为 1024 是用时 3295 长度为 10240000 时用时 2942 长度为 1024000000 时用时 3485
图片大小是 52mb
为什么数组长度变大了,读取时间反而是长→短→更长呢?
大佬有話說 (12)