代码如下:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Textcopy {
public static void main(String[] args) {
copy1();
copy2();
}
//普通文件流
public static void copy1() {
File file = new File("C:\\KuGou\\纯音乐 - Love9 (钢琴版).mp3");
FileInputStream in = null;
long startTime = System.currentTimeMillis();//获取任务起始时间
try {
in = new FileInputStream(file);
byte[] b_in = new byte[1024];
try {
while (in.read(b_in) != -1) {
}
long endTime = System.currentTimeMillis();//获取任务终止时间
System.out.println("任务耗时:" + (endTime - startTime) + "秒");
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//缓存流
public static void copy2() {
File file = new File("C:\\\\KuGou\\\\纯音乐 - Love9 (钢琴版).mp3");
FileInputStream in = null;
BufferedInputStream bfin = null;
long startTime = System.currentTimeMillis();//获取读取任务的起始时间
try {
in = new FileInputStream(file);
bfin = new BufferedInputStream(in);//包装输入字节流
byte[] b_in = new byte[1024];
try {
while (bfin.read(b_in) != -1) {
}
long endTime = System.currentTimeMillis();//获取任务终止时间
System.out.println("任务耗时:" + (endTime - startTime) + "秒");
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bfin != null) {
try {
bfin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}