Pionner17
2017-12-20 15:20
/**
* 批量读写
*
* @param srcFile
* @param destFile
*/
public static void copyFile(File srcFile, File destFile) {
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
if (!srcFile.exists()) {
throw new IllegalArgumentException("源目标文件不存在!!!");
}
if (!srcFile.isFile()) {
throw new IllegalArgumentException("源目标不是文件类型!!!");
}
try {
inputStream = new FileInputStream(srcFile);
outputStream = new FileOutputStream(destFile);
byte[] bytes = new byte[8 * 1024];
int length = 0;
while ((length = inputStream.read(bytes, 0, bytes.length)) != -1) {
outputStream.write(bytes, 0, length);
outputStream.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 缓冲批量读写
*
* @param srcFile
* @param destFile
*/
public static void copyFileByBuffer(File srcFile, File destFile) {
FileInputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
FileOutputStream outputStream = null;
BufferedOutputStream bufferedOutputStream = null;
if (!srcFile.exists()) {
throw new IllegalArgumentException("源目标文件不存在!!!");
}
if (!srcFile.isFile()) {
throw new IllegalArgumentException("源目标不是文件类型!!!");
}
try {
inputStream = new FileInputStream(srcFile);
bufferedInputStream = new BufferedInputStream(inputStream);
outputStream = new FileOutputStream(destFile);
bufferedOutputStream = new BufferedOutputStream(outputStream);
byte[] bytes = new byte[8 * 1024];
int length = 0;
while ((length = bufferedInputStream.read(bytes, 0, bytes.length)) != -1) {
bufferedOutputStream.write(bytes, 0, length);
outputStream.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedOutputStream.close();
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}测试代码:
public class IOUtilTest {
public static void main(String[] args) throws IOException {
File srcFile1 = new File("F:\\学习资料\\1-1和2.mp4");
File destFile1 = new File("F:\\1.mp4");
long beg1 = System.currentTimeMillis();
IOUtil.copyFile(srcFile1,destFile1);
long end1 = System.currentTimeMillis();
System.out.println("批量读取时间毫秒:"+(end1 - beg1));//768
File destFile2 = new File("F:\\2.mp4");
long beg2 = System.currentTimeMillis();
IOUtil.copyFileByBuffer(srcFile1,destFile2);
long end2 = System.currentTimeMillis();
System.out.println("批量缓冲读取时间毫秒:"+(end2 - beg2));//130
}
}源目标文件大小:60.4M
这里的对比条件并不成立,并不能说谁比谁快
而是理解缓冲是为了减少I/O,可以理解为减少写次数,比如读出一个字节就去写入,和全部读出放入缓冲区存储最后一次写入,当时是后者更好
上面的同学举的例子:
在批量读取、和缓冲批量读中均是全部读出一次写入,其实并没有用到缓冲的优势
理论上是不通的。
按道理应该是批量比缓冲更快。可以看一下源码,知道缓冲底层调的是批量。如果数据不对,试一下多试几次。有可能是JVM垃圾收集影响了。还有要拷贝不同的文件。读过的文件操作系统有缓存了。拷一个1-2GB的文件试试。
public static void main(String[] args) throws IOException { File srcFile1 = new File("logs/input.iso"); File destFile1 = new File("logs/output.iso"); long beg1 = System.currentTimeMillis(); copyFileByBatch(srcFile1,destFile1); long end1 = System.currentTimeMillis(); System.out.println("批量读取时间毫秒:"+(end1 - beg1));//1261 File srcFile2 = new File("logs/input2.iso"); File destFile2 = new File("logs/output2.iso"); long beg2 = System.currentTimeMillis(); copyFileByBuffer(srcFile2,destFile2); long end2 = System.currentTimeMillis(); System.out.println("批量缓冲读取时间毫秒:"+(end2 - beg2));//1899 }
我看的时候也在想这个问题。谢谢给出答案!
受教了,我刚才还闹不清楚批量比缓冲快还用缓存干嘛,现在想明白啦。闹混了对比的对象应该是单字节跟缓冲对比,批量跟批量缓冲对比。
因为用了缓存~
文件传输基础——Java IO流
133841 学习 · 1060 问题
相似问题