1. 程式人生 > >Java-簡易帶進度條的資料夾複製

Java-簡易帶進度條的資料夾複製

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());                                             }                 }    

            }

        });

    }

}