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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值