java基础——swing中的文件复制

package gui;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.util.Random;

public class TestGUI {

    static long allFileSize = 0;
    static long currentFileSizeCopied = 0;

    public static void calculateAllFileSize(File file){
        if(file.isFile()){
            allFileSize += file.length();
        }
        if(file.isDirectory()){
            File[] fs = file.listFiles();
            for(File f:fs){
                calculateAllFileSize(f);
            }
        }
    }

    public static void main(String[] args){
        JFrame f = new JFrame("lol");
        f.setSize(400,300);
        f.setLocation(200,200);
        f.setLayout(new BorderLayout());
        JPanel pNorth = new JPanel();
        pNorth.setLayout(new FlowLayout());

        JLabel src = new JLabel();
        src.setText("源文件地址:");
        JTextField srcText = new JTextField("");
        srcText.setText("d:/test/pics");
        srcText.setPreferredSize(new Dimension(80,30));

        JLabel dest = new JLabel();
        dest.setText("复制到:");
        JTextField destText = new JTextField("");
        destText.setText("d:/test/picscopy");
        destText.setPreferredSize(new Dimension(80,30));

        JPanel pSouth = new JPanel();
        pSouth.setLayout(new FlowLayout());
        JButton b = new JButton("开始复制");
        b.setPreferredSize(new Dimension(140,30));

        JLabel l = new JLabel("文件复制进度:");

        pSouth.add(b);
        pSouth.add(l);

        pNorth.add(src);
        pNorth.add(srcText);
        pNorth.add(dest);
        pNorth.add(destText);

        f.add(pNorth,BorderLayout.NORTH);
        f.add(pSouth,BorderLayout.CENTER);

        JProgressBar pb = new JProgressBar();
        pb.setMaximum(100);
        pb.setStringPainted(true);
        pSouth.add(pb);

        //f.add(pb);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

        String srcPath = srcText.getText();
        File folder = new File(srcPath);
        calculateAllFileSize(folder);

        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                currentFileSizeCopied = 0;
                String srcPath = srcText.getText();
                String destPath = destText.getText();
                new Thread(()->copyFolder(srcPath,destPath)).start();
                b.setEnabled(false);

            }

            public void copyFile(String srcPath,String destPath){
                File srcFile = new File(srcPath);
                File destFile = new File(destPath);

                byte[] buffer = new byte[1024];
                try(FileInputStream fis = new FileInputStream(srcFile);
                FileOutputStream fos = new FileOutputStream(destFile);){
                    while(true){
                        int actuallyReaded = fis.read(buffer);
                        if(-1==actuallyReaded){
                            break;
                        }
                        fos.write(buffer,0,actuallyReaded);
                        fos.flush();
                    }
                }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 = currentFileSizeCopied/allFileSize;

                        int progress = (int)(current*100);
                        pb.setValue(progress);
                        if(progress==100){
                            JOptionPane.showMessageDialog(f,"复制完毕");
                            b.setEnabled(true);
                        }
                    }

                    if(srcFile.isDirectory()){
                        File newDestFolder = new File(destFolder,srcFile.getName());
                        copyFolder(srcFile.getAbsolutePath(),newDestFolder.getAbsolutePath());
                    }
                }
            }
        });
    }
}

你可能感兴趣的:(java基础)