第四次作业 文件复制速度的提升
请教了很多才得出这个还算有效的方法,虽然不是我自己想出来的,但是我记住了,我觉得自己还是缺乏独立思维的能力所以才每次做作业都很吃力吧,希望会越来越有自己的思维。
原代码为:
package proj;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy {
public static void main(String []args){
try {
FileInputStream fis =new FileInputStream("a.mp3");
FileOutputStream fos =new FileOutputStream("a.mp3");
int read=fis.read();
while(read !=-1){
fos.write(read);
read=fis.read();
}
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
long startTime = 0;
System.out.println("用时: " + (endTime - startTime) + " ms");
}
}
修改的代码部分:
byte[] buf = new byte[1024]; int len = 0 ;//提高读取字节速度 while((len=fis.read(buf)) != -1){ fos.write(buf,0,len);}


浙公网安备 33010602011771号