Fork me on GitHub

文件夹复制进度条

使用Java写出的简陋GUI,显示文件夹复制的进度条。

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
public class TestGUI {
static long allFileSize = 0; // 所有需要复制的文件大小
static long currentFileSizeCopied = 0;// 已复制的文件总大小
/**
* 遍历文件夹获取文件夹内容总大小
*
* @param file
*/
public static void calclateAllFilesize(File file) {
if (file.isFile()) {
allFileSize += file.length();
}
if (file.isDirectory()) {
File[] fs = file.listFiles();
for (File f : fs) {
calclateAllFilesize(f);
}
}
}
public static void main(String[] args) {
JFrame f = new JFrame("带进度条的文件夹复制");
f.setSize(450, 140);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
// 文件地址
JLabel lStr = new JLabel("源文件地址:");
JTextField strTf = new JTextField("");
strTf.setText("e:/JDK");
strTf.setPreferredSize(new Dimension(100, 30));
JLabel lDest = new JLabel("复制到:");
JTextField destTf = new JTextField("");
destTf.setText("e:/JDK2");
destTf.setPreferredSize(new Dimension(100, 30));
f.add(lStr);
f.add(strTf);
f.add(lDest);
f.add(destTf);
JButton bStartCopy = new JButton("开始复制");
bStartCopy.setPreferredSize(new Dimension(100, 30));
JLabel l = new JLabel("文件复制进度:");
JProgressBar pb = new JProgressBar();
pb.setMaximum(100);
pb.setStringPainted(true);
f.add(bStartCopy);
f.add(l);
f.add(pb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
// 计算需要复制的文件的总大小
String srcPath = strTf.getText();
File folder = new File(srcPath);
calclateAllFilesize(folder);
// 点击开始复制
bStartCopy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentFileSizeCopied = 0;
String srcPath = strTf.getText();
String destPath = destTf.getText();
new Thread(() -> copyFolder(srcPath, destPath)).start();
bStartCopy.setEnabled(false);
}
public void copyFile(String srcPath, String destPath) {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
// 缓存区,一次性读取1024字节
byte[] buffer = new byte[1024];
try (FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);) {
while (true) {
// 实际读取的长度是 actuallyReaded,有可能小于1024
int actuallyReaded = fis.read(buffer);
// -1表示没有可读的内容了
if (-1 == actuallyReaded)
break;
fos.write(buffer, 0, actuallyReaded);
fos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void copyFolder(String srcPath, String destPath) {
File srcFolder = new File(srcPath);
File destFolder = new File(destPath);
if (!srcFolder.exists())
return;
if (!srcFolder.isDirectory())
return;
if (destFolder.isFile())
return;
if (!destFolder.exists())
destFolder.mkdirs();
File[] files = srcFolder.listFiles();
for (File srcFile : files) {
if (!(srcFile.isDirectory())) {
File newDestFile = new File(destFolder, srcFile.getName());
copyFile(srcFile.getAbsolutePath(), newDestFile.getAbsolutePath());
currentFileSizeCopied += srcFile.length();
double current = (double) currentFileSizeCopied / (double) allFileSize;
int progress = (int) (current * 100);
pb.setValue(progress);
if (progress == 100) {
JOptionPane.showMessageDialog(f, "复制完毕");
bStartCopy.setEnabled(true);
}
}
if (srcFile.isDirectory()) {
File newDestFolder = new File(destFolder, srcFile.getName());
copyFolder(srcFile.getAbsolutePath(), newDestFolder.getAbsolutePath());
}
}
}
});
}
}

Your support will encourage me to continue to create!