1.OIO(BIO)复制文件代码
public static void blockCopyFile(){
//复制文件39.4G ,共计时间 171s
InputStream input = null;
OutputStream output = null;
File srcFile = new File("C:\\Users\\zxiong\\Downloads\\gradle-7.0_1.zip");
File destFile = new File("C:\\Users\\zxiong\\Downloads\\gradle-7.0_9.zip");
try {
//如果目标文件不存在,则新建
if (!destFile.exists()) {
destFile.createNewFile();
}
long startTime = System.currentTimeMillis();
input = new FileInputStream(srcFile);
output = new FileOutputStream(destFile);
byte[] buf = new byte[1024*1024*100];
int bytesRead;
while ((bytesRead = input.read(buf)) != -1) {
output.write(buf, 0, bytesRead);
}
output.flush();
long endTime = System.currentTimeMillis();
log.debug("IO流复制秒数:" + (endTime - startTime)/1000);
} catch (IOException e) {
e.printStackTrace();
} finally {
IoUtil.close(input);
IoUtil.close(output);
}
}
2.NIO复制文件代码一
public static void test5() throws Exception{
//400M/S 149s 39.4G
FileChannel in4 = new FileInputStream("C:\\Users\\zxiong\\Downloads\\gradle-7.0_1.zip").getChannel();
FileChannel out4 = new FileOutputStream("C:\\Users\\zxiong\\Downloads\\gradle-7.0_5.zip").getChannel();
long size = in4.size();
ByteBuffer bb = ByteBuffer.allocate(1024*1024);
int length;
long s = 0;
long w = 0;
Instant start = Instant.now();
NumberFormat numberFormat = NumberFormat.getInstance();
while((length = in4.read(bb)) != -1){
s += length;
String sFormat = numberFormat.format((float) s / (float) size * 100);
log.info("文件读取进度:{}%", sFormat);
for (int j = 0; j <= String.valueOf(sFormat).length(); j++) {
System.out.print("\b");
}
bb.flip();
int len = 0;
while ((len = out4.write(bb)) != 0){
w += len;
String wFormat = numberFormat.format((float) w / (float) size * 100);
log.info("文件写入进度:{}%", wFormat);
for (int j = 0; j <= String.valueOf(wFormat).length(); j++) {
System.out.print("\b");
}
}
bb.clear();
}
in4.close();
out4.close();
Instant end = Instant.now();
long seconds = Duration.between(start, end).getSeconds();
log.info("文件第五次复制用时(秒):{}",seconds);
}
3.NIO复制文件代码二
public static void test6() throws Exception{
//250M/S 161s 39.4G
FileChannel in4 = new FileInputStream("C:\\Users\\zxiong\\Downloads\\gradle-7.0_1.zip").getChannel();
FileChannel out4 = new FileOutputStream("C:\\Users\\zxiong\\Downloads\\gradle-7.0_5.zip").getChannel();
long size = in4.size();
ByteBuffer bb = ByteBuffer.allocate(1024*1024*512);
int length;
long s = 0;
long w = 0;
Instant start = Instant.now();
NumberFormat numberFormat = NumberFormat.getInstance();
while((length = in4.read(bb)) != -1){
s += length;
String sFormat = numberFormat.format((float) s / (float) size * 100);
log.info("文件读取进度:{}%", sFormat);
for (int j = 0; j <= String.valueOf(sFormat).length(); j++) {
System.out.print("\b");
}
bb.flip();
int len = 0;
while ((len = out4.write(bb)) != 0){
w += len;
String wFormat = numberFormat.format((float) w / (float) size * 100);
log.info("文件写入进度:{}%", wFormat);
for (int j = 0; j <= String.valueOf(wFormat).length(); j++) {
System.out.print("\b");
}
}
bb.clear();
}
in4.close();
out4.close();
Instant end = Instant.now();
long seconds = Duration.between(start, end).getSeconds();
log.info("文件第六次复制用时(秒):{}",seconds);
}
3.NIO复制文件代码三
public static void test1() throws Exception{
//39.4G 140s
Instant start = Instant.now();
FileChannel in1 = new FileInputStream("C:\\Users\\zxiong\\Downloads\\gradle-7.0_2.zip").getChannel();
FileChannel out1 = new FileOutputStream("C:\\Users\\zxiong\\Downloads\\gradle-7.0_1.zip").getChannel();
try {
long size = in1.size();
log.info("size:{}",size);
out1.transferFrom(in1, 0, size);
} catch (IOException e) {
e.printStackTrace();
}
Instant end = Instant.now();
long seconds = Duration.between(start, end).getSeconds();
log.info("文件第一次复制时间(秒):{}",seconds);
}
4.当前测试环境
interl i7 4核 内存8G
5.总结
在大文件传输上,NIO比BIO传输速率更快一些,以上示例还没有运用到多线程传递数据
网友评论