为什么我用缓冲流速度反而慢了
不使用缓冲流
public static void fileCopy(String filePath, String toPath) {
File copy = new File(filePath);
File paste = new File(toPath);
FileInputStream input = null;
FileOutputStream output = null;
try {
if (!copy.exists()) {
System.out.println("文件不存在");
} else {
if (!paste.exists()) {
paste.mkdirs();
} else if (paste.exists() && paste.isFile()) {
System.out.println("目标路径与文件重名");
return;
}
File temp = new File(paste, copy.getName());
// 处理同名文件
for (int i = 1; temp.exists(); i++) {
temp = new File(paste, copy.getName().substring(0, (copy.getName().lastIndexOf('.'))) + "(" + i
+ ")" + copy.getName().substring(copy.getName().lastIndexOf('.')));
}
temp.createNewFile();
input = new FileInputStream(copy);
output = new FileOutputStream(temp);
byte[] b = new byte[1024];
int n = 0;
long startTime = System.currentTimeMillis();
while (input.read(b) != -1) {
output.write(b,0,n);
}
long endTime = System.currentTimeMillis();
System.out.println("复制文件成功");
System.out.println("用时" + (endTime - startTime));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
input.close();
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}使用缓冲流
public static void filecopy(String filePath, String toPath) {
File copy = new File(filePath);
File paste = new File(toPath);
// 判断目标文件不存在 结束
if (!copy.exists()) {
System.out.println("文件不存在");
return;
}
// 目标路径不存在 创建
if (!paste.exists()) {
paste.mkdirs();
}
// 目标路径存在且是文件 结束
else if (paste.exists() && paste.isFile()) {
System.out.println("目标路径与文件重名");
return;
}
try {
// 处理同名文件
File temp = new File(paste, copy.getName());
for (int i = 1; temp.exists(); i++) {
temp = new File(paste, copy.getName().substring(0, (copy.getName().lastIndexOf('.'))) + "(" + i + ")"
+ copy.getName().substring(copy.getName().lastIndexOf('.')));
}
// 使用buffered复制文件
BufferedInputStream inputstream = new BufferedInputStream(new FileInputStream(copy));
BufferedOutputStream outputstream = new BufferedOutputStream(new FileOutputStream(temp));
int n = 0;
long startTime = System.currentTimeMillis();
while ((n = inputstream.read()) != -1) {
outputstream.write(n);
}
long endTime = System.currentTimeMillis();
outputstream.close();
inputstream.close();
System.out.println("复制文件成功");
System.out.println("用时" + (endTime - startTime));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}调用代码
public static void main(String[] args) {
Copy.fileCopy("D:\\Java\\微信图片_20190812160359.jpg", "D:\\kayang\\A");
BufferedCopy.filecopy("D:\\Java\\微信图片_20190812160359.jpg", "D:\\kayang\\A");
}结果

31
收起
正在回答
1回答
同学你好,因为同学使用缓冲流定义的是数组,而使用缓冲流读取时却没有用数组读取,如下


建议同学使用一样的方式去读取文件
如下,不使用缓冲流

使用缓冲流:

运行结果如下

祝学习愉快
java工程师2020版
- 参与学习 人
- 提交作业 9410 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星