javaSwing+线程实现文件的复制进度条显示+暂停与恢复

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

class ProgressMonitorTest extends UserMonitorFrame {
    static float process = 0;

    public void useProgressMonitor(JFrame frame, String copyPath, String newPath) {
        try {
            File file = new File(copyPath); // 根据要复制的文件创建File对象
            File newFile = new File(newPath); // 根据复制后文件的保存地址创建File对象
            FileOutputStream fop = new FileOutputStream(newFile); // 创建FileOutputStream对象
            InputStream in = new FileInputStream(file);
            // 读取文件,如果总耗时超过2秒,将会自动弹出一个进度监视窗口。
            JProgressBar progressBar = new JProgressBar();
            progressBar.setStringPainted(true);
            progressBar.setBounds(100, 100, 200, 20);
            frame.add(progressBar);
            int all = in.available();
            int reader = 0;
            int c = 0;
            byte[] bytes = new byte[1024]; // 定义byte数组
            while ((c = in.read(bytes)) != -1) { // 循环读取文件
                if (isContinue) {
                    thread.stop();
                }
                else {
                    fop.write(bytes, 0, c); // 通过流写数据
                    reader = c + reader;
                    process = (int) ((float) reader / all * 100);
                    progressBar.setValue((int) process);
                    if (process==99){
                        JOptionPane.showMessageDialog(frame,"复制文件成功");
                        break;
                    }
                }}
            fop.close(); // 关闭输出流
            in.close(); // 关闭输入流
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


}

class UserMonitorFrame extends JFrame implements Runnable {
    /**
     *
     */
    public static boolean isContinue = false;
    Thread thread = new Thread(this);
    public JButton jButton = new JButton();
    private JPanel contentPane;
    private JTextField fileField;
    private JTextField searchTextField;
    private JTextField replaceTextField;
    private File file;
    private JTextField textField;
    private JTextField textField_1;
    static int count = 0;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UserMonitorFrame frame = new UserMonitorFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public UserMonitorFrame() {
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 501, 184);
        setTitle("在读取文件时使用进度条");
        getContentPane().setLayout(null);

        JLabel label = new JLabel("文件地址");
        label.setBounds(10, 10, 70, 15);
        getContentPane().add(label);

        textField = new JTextField();
        textField.setBounds(90, 7, 300, 21);
        getContentPane().add(textField);
        textField.setColumns(10);

        JButton button = new JButton("选择文件");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        button.setBounds(400, 6, 93, 23);
        getContentPane().add(button);

        JLabel label_1 = new JLabel("复制地址");
        label_1.setBounds(10, 40, 70, 15);
        getContentPane().add(label_1);

        textField_1 = new JTextField();
        textField_1.setBounds(90, 38, 300, 21);
        getContentPane().add(textField_1);
        textField_1.setColumns(10);

        JButton button_1 = new JButton("选择地址");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_1_actionPerformed(e);
            }
        });
        button_1.setBounds(400, 39, 93, 23);
        getContentPane().add(button_1);

        JButton button_2 = new JButton("复制");

        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                jButton.setText("暂停");
                jButton.setBounds(300, 100, 70, 23);
                getContentPane().add(jButton);
                do_copyButton_actionPerformed(e);
                jButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (count % 2 == 0) {
                            count++;
                            setContinue();
                            jButton.setText("恢复");
                            System.out.println(count);
                        } else {
                            jButton.setText("暂停");
                            count++;
                            setContinue();
                            System.out.println(count);
                        }
                    }
                });
            }
        });
        button_2.setBounds(182, 69, 93, 23);
        getContentPane().add(button_2);
    }

    protected void do_button_actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        // 显示文件打开对话框
        int option = chooser.showOpenDialog(this);
        // 确定用户是否选择了文件
        if (option != JFileChooser.APPROVE_OPTION)
            return;
        // 获取用户选择的文件对象
        file = chooser.getSelectedFile();
        // 显示文件信息到文本框
        textField.setText(file.toString());
    }

    protected void do_button_1_actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int option = chooser.showOpenDialog(this);
        if (option != JFileChooser.APPROVE_OPTION)
            return;
        file = chooser.getSelectedFile();
        textField_1.setText(file.toString());
    }

    //确定复制按钮单击事件
    protected void do_copyButton_actionPerformed(ActionEvent arg0) {
//        Thread thread = new Thread(this);
        thread.start();
    }

    //应用多线程技术实现读取操作
    @Override
    public void run() {
        ProgressMonitorTest test = new ProgressMonitorTest();
        //分割出文件名
        String regex = "\\\\";
        String path = textField.getText();
        String[] digitWord = path.split(regex);
        String p = digitWord[digitWord.length - 1];
        String save = textField_1.getText();
        test.useProgressMonitor(this, path, save + "\\" + p + path.substring(path.lastIndexOf("."), path.length()));

    }
    public void setContinue() {
        if (count % 2 == 1)
            this.isContinue = true;
        else
            this.isContinue = false;
    }
}

差不多也就长这样
在这里插入图片描述
java中线程的stop停止操作不是说已经停用了吗,但是发现这里好像还可以用。。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值