package HH;
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 HP {
/**
* @param args
*/
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) {
// TODO Auto-generated method stub
final JFrame f=new JFrame("带进度条的文件夹复制");
f.setSize(450, 140);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JLabel l=new JLabel("源文件地址:");
final JTextField jd=new JTextField("");
jd.setText("D:/WeChat");
jd.setPreferredSize(new Dimension(100, 30));
JLabel label=new JLabel("复制到:");
final JTextField jf=new JTextField("");
jf.setText("D:/Download");
jf.setPreferredSize(new Dimension(100,30));
f.add(l);
f.add(jd);
f.add(label);
f.add(jf);
final JButton jButton=new JButton("开始复制");
jButton.setPreferredSize(new Dimension(100,30));
JLabel label2=new JLabel("文件复制进度");
final JProgressBar jr=new JProgressBar();
jr.setMaximum(100);
jr.setValue(0);
jr.setStringPainted(true);
f.add(jButton);
f.add(label2);
f.add(jr);
f.setVisible(true);
// 计算需要复制的文件的总大小
String srcpath=jd.getText();
File folder=new File(srcpath);
calclateAllFilesize(folder);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
currentFileSizeCopied=0;
final String srcpath=jd.getText();
final String destpath=jf.getText();
new Thread(){
public void run(){
copyFolder(srcpath,destpath);
}
}.start();
jButton.setEnabled(false);
}
private void copyFile(String srcpath, String destpath) {
// TODO Auto-generated method stub
File srcfile=new File(srcpath);
File destFile=new File(destpath);
byte[] buffer=new byte[1024];
try {
FileInputStream fim=new FileInputStream(srcfile);
FileOutputStream fos=new FileOutputStream(destFile);
while(true){
int ls=fim.read(buffer);
// -1表示没有可读的内容了
if(-1==ls){
break;
}
fos.write(buffer, 0, ls);
fos.flush();
}
fim.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}catch (IOException e1) {
// TODO: handle exception
e1.printStackTrace();
}
}
private void copyFolder(String srcpath, String destpath) {
// TODO Auto-generated method stub
File srcfile=new File(srcpath);
File destFile=new File(destpath);
if(!srcfile.exists()){
return;
}
if(!srcfile.isDirectory()){
return;
}
if(destFile.isFile()){
return;
}
if(!destFile.exists()){
destFile.mkdirs();
}
File[] files=srcfile.listFiles();
for(File fs:files){
if(!fs.isDirectory()){
File dest=new File(destFile,fs.getName());
copyFile(fs.getAbsolutePath(), dest.getAbsolutePath());
currentFileSizeCopied+=fs.length();
double current=(double)currentFileSizeCopied/(double)allFileSize;
int progress=(int) (current*100);
jr.setValue(progress);
if(progress==100){
JOptionPane.showMessageDialog(f, "复制完毕");
jButton.setEnabled(true);
}
}
if(srcfile.isDirectory()){
File fil=new File(destFile,srcfile.getName());
copyFile(fs.getAbsolutePath(), fil.getAbsolutePath());
}
}
}
});
}
}