App_FileCopier

package studio.sm.homework;



import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.DataInputStream;

import java.io.DataOutputStream;

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.JCheckBox;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JProgressBar;

import javax.swing.Timer;



 



public class App_FileCopy implements ActionListener,Runnable

{

    JFrame frame;

    JProgressBar progressbar;

    JCheckBox jChkBox;

    Timer timer;

    JButton btn;

    

       File file_src;

       File file_des_folder;

       

       Thread thread;

 

    public App_FileCopy() {



       frame = new JFrame("App_FileCopier");

       frame.setBounds(300, 300, 400, 130);

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       frame.setResizable(false);

       Container contentPanel = frame.getContentPane();

       progressbar = new JProgressBar();

       progressbar.setOrientation(JProgressBar.HORIZONTAL);

       //progressbar.setMinimum(0);

       //progressbar.setMaximum(100);

       //progressbar.setValue(0);

       progressbar.setStringPainted(true);

       //progressbar.setPreferredSize(new Dimension(300, 20));

       progressbar.setBorderPainted(true);



       JPanel panel = new JPanel();



       btn = new JButton("复制");

       btn.addActionListener(this);

       

       jChkBox = new JCheckBox("显示进度");

       jChkBox.setSelected(true);

       

       //--> jChkBox's ActionListener

       jChkBox.addActionListener(new ActionListener()

       {



        @Override

        public void actionPerformed(ActionEvent arg0)

        {

            if(jChkBox.isSelected()) progressbar.setVisible(true);

            else progressbar.setVisible(false);

            

        }});

       

            

       panel.add(btn);

       panel.add(jChkBox);

       //timer = new Timer(100, this);



       contentPanel.add(panel, BorderLayout.NORTH);



       contentPanel.add(progressbar, BorderLayout.SOUTH);



       frame.setVisible(true);



    }



 



    public void actionPerformed(ActionEvent e) 

    {    

       if (e.getSource() == btn) 

       {

           

               JFileChooser file_src_chooser = new JFileChooser();

               file_src_chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

               

               JFileChooser folder_des_chooser = new JFileChooser();

               folder_des_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

               

               file_src_chooser.setDialogTitle("请选择您要复制的文件");

               folder_des_chooser.setDialogTitle("请选择您要复制到的路径");

              

               int whichButton = file_src_chooser.showOpenDialog(null);

            if (whichButton == JFileChooser.APPROVE_OPTION)

            {  

                //path = file_src.getSelectedFile().getAbsolutePath();

                file_src = file_src_chooser.getSelectedFile();

                System.out.println("* Source file -> " + file_src.toString());

                System.out.println("* --> the src file's length is " + file_src.length() + " bytes");

                

                int whichBTN = folder_des_chooser.showOpenDialog(null);

                

                

                //--> okay, get everything done

                if(whichBTN == JFileChooser.APPROVE_OPTION)

                {

                    file_des_folder = folder_des_chooser.getSelectedFile();

                    System.out.println("* Destination -> " + file_des_folder.toString());

                    //fileCopy(file_src,file_des_folder);

                    

                    thread = new Thread(this);  

                    thread.start(); 

                }

                

                

                else if(whichBTN == JFileChooser.CANCEL_OPTION)

                {

                    System.out.println("* The user clicked button cancel on this DIRECTORY chooser dialog.");

                }

                

            }

            else if (whichButton == JFileChooser.CANCEL_OPTION)

            {

                System.out.println("* The user clicked button cancel on this FILE chooser dialog.");

            }

                   

       }

//       if (e.getSource() == timer) 

//       {

//           int value = progressbar.getValue();

//           if (value < 100)

//              progressbar.setValue(++value);

//           else 

//           {

//              timer.stop();

//              //frame.dispose();

//           }

//       }

    }



 

    public void fileCopy(File src,File des)  //--> runs in a thread

    {

        //long file_length = src.length();



        

        //System.out.println("* --> the src file's length is " + file_length + " bytes");

 //       if (file_length >= 100)

//        {

            //timer.start();

//            System.out.println("* --> src file is larger than 100 bytes");

//            progressbar.setMinimum(0);

//            progressbar.setMaximum((int)file_length);

//            progressbar.setValue(0);

 //       }

        

        

        File des_file = new File(des.toString() + File.separator + src.getName());

        System.out.println("* after combination,finally -> " + des_file.toString());

               

        try

        {

            BufferedInputStream bis = new BufferedInputStream(new DataInputStream(new FileInputStream(src)));

            BufferedOutputStream bos = new BufferedOutputStream(new DataOutputStream(new FileOutputStream(des_file)));

            

            byte[] readBytes = new byte[1024];

            

            int size = bis.available();

            System.out.println("* --> the size of the src file is " + size + " bytes");

            

            size = size - (size % 1024) + 1024; 

            float per = (float) (100 / (size / 1024.0)); 

            int i = 0;

            

            while (true)

            {  

                int len = bis.read(readBytes);  

                if (len == -1)

                {

                    progressbar.setValue(100);

                    JOptionPane.showMessageDialog(null, des_file.getName() + " 文件已经复制完成.");

                    progressbar.setValue(0);

                    break;

                }

                    

                bos.write(readBytes, 0, len);

                bos.flush();

                progressbar.setValue((int)(per * i ));  

                //progressBarPanel.addEvent(e);  

                i++;  

            }  

            

//            while ((length = bis.read(readBytes, 0, readBytes.length)) > 0) 

//            {          

//                

//                bos.write(readBytes, 0, length);

//                bos.flush();

//            }

                      

            System.out.println("* --> File copy completed.");

            bos.close();

            bis.close();

                        

        } catch (FileNotFoundException e)

        {

            e.printStackTrace();

        } catch (IOException e)

        {



            e.printStackTrace();

        }      

    }

    

    

     

    public static void main(String[] args)

    {

        new App_FileCopy();

    }





    @Override

    public void run()

    {    

        fileCopy(file_src,file_des_folder);

        

    }

}

 

你可能感兴趣的:(copier)