kim.zhang

风在前,无惧!


  • 首页

  • 标签42

  • 分类12

  • 归档94

  • 搜索

文件复制.md

发表于 2020-08-10 更新于 2021-11-21 分类于 网络编程
本文字数: 3.9k 阅读时长 ≈ 4 分钟

四种复制文件的方法

  • 字节复制
  • 缓冲流复制
  • 使用Buffer的FileChanel
  • 直接使用FileChannel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* @作者 ming
* @目标 当你的才华还撑不起你的野心时,就应该静下心来学习;
* 当你的能力还驾驭不了你的目标时,就应该沉下心来历练;
* 梦想不是浮躁,而是沉淀和积累。
* @创建时间 2020/8/9 14:29
*/
package filecopy;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;


public class FileCopyTest {
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
FileCopyRunner runner1 = (source, target) -> {
InputStream fin = null;
OutputStream fout = null;
try {
fin = new FileInputStream(source);
fout = new FileOutputStream(target);
int size = 0;
while ((size = fin.read()) != -1) {
fout.write(size);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
close(fin);
close(fout);
}
};
long startTime = System.currentTimeMillis();
File sourceFile1 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\test.doc");
File targetFile1 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\runner1.doc");
runner1.copyFile(sourceFile1, targetFile1);
System.out.println("runner1总耗时:" + (System.currentTimeMillis() - startTime));

FileCopyRunner runner2 = (source, target) -> {
InputStream fin = null;
OutputStream fout = null;
try {
fin = new BufferedInputStream(new FileInputStream(source));
fout = new BufferedOutputStream(new FileOutputStream(target));
byte[] buffer = new byte[1024];
while ((fin.read(buffer)) != -1) {
fout.write(buffer, 0, buffer.length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
close(fin);
close(fout);
}
};
startTime = System.currentTimeMillis();
File sourceFile2 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\test.doc");
File targetFile2 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\runner2.doc");
runner1.copyFile(sourceFile2, targetFile2);
System.out.println("runner2总耗时:" + (System.currentTimeMillis() - startTime));

FileCopyRunner runner3 = (source, target) -> {
FileChannel fin = null;
FileChannel fout = null;

try {
fin = new FileInputStream(source).getChannel();
fout = new FileOutputStream(target).getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (fin.read(buffer) != -1) {
// 切换为写模式
buffer.flip();
while (buffer.hasRemaining()) {
fout.write(buffer);
}
// 切换为读模式
buffer.clear();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
close(fin);
close(fout);
}
};
startTime = System.currentTimeMillis();
File sourceFile3 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\test.doc");
File targetFile3 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\runner3.doc");
runner1.copyFile(sourceFile3, targetFile3);
System.out.println("runner3总耗时:" + (System.currentTimeMillis() - startTime));

FileCopyRunner runner4 = (source, target) -> {
FileChannel fin = null;
FileChannel fout = null;

try {
fin = new FileInputStream(source).getChannel();
fout = new FileOutputStream(target).getChannel();
long totalSize = 0;
while (totalSize != fin.size()) {
totalSize += fin.transferTo(0, fin.size(), fout);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
close(fin);
close(fout);
}
};
startTime = System.currentTimeMillis();
File sourceFile4 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\test.doc");
File targetFile4 = new File("D:\\IDEA Pro\\sockets\\src\\filecopy\\runner4.doc");
runner1.copyFile(sourceFile4, targetFile4);
System.out.println("runner4总耗时:" + (System.currentTimeMillis() - startTime));
}
}
一毛也是爱~
Kim.Zhang 微信支付

微信支付

# 文件复制
Conditional.md
面向抽象编程.md
  • 文章目录
  • 站点概览
Kim.Zhang

Kim.Zhang

且行且珍惜
94 日志
12 分类
42 标签
E-Mail Weibo
  1. 1. 四种复制文件的方法
粵ICP备19091267号 © 2019 – 2022 Kim.Zhang | 629k | 9:32
本站总访问量 4 次 | 有 309 人看我的博客啦 |
博客全站共176.7k字
载入天数...载入时分秒...
0%