java实现在复制文件时使用进度条(java实现进度条)

2022-10-21,,,

思路分析:

因为既要有操作面板又要有进度条,所以肯定要出现两个继承jframe类的窗体。
先看被调用的进度条窗体,它不需要手动操作,所以类的内部实现一个方法就可以了。因为设计文件操作,所以要捕获异常。首先根据要复制的文件创建file对象,以及根据复制后文件的保存地址创建file对象,然后创建fileoutputstream对象,再创建fileinputstream对象,之后是progressmonitorinputstream对象,然后读取文件,如果总耗时超过2秒,将会自动弹出一个进度监视窗口。接下来定义byte数组,再使用while循环读取文件,使用fileoutputstream类的write()方法通过流写数据,再使用fileoutputstream类的close()方法关闭输出流,最后使用progressmonitorinputstream类的close()方法关闭输入流。可见该方法需要三个参数:弹出它的父窗口、要复制的文件地址以及要复制到的文件夹。
代码如下:

progressmonitortest.java:

复制代码 代码如下:
package cn.edu.xidian.crytoll;
import java.io.fileinputstream;
import java.io.*;
import javax.swing.jframe;
import javax.swing.progressmonitorinputstream;
public class progressmonitortest {

    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秒,将会自动弹出一个进度监视窗口。
            progressmonitorinputstream pm = new progressmonitorinputstream(
                    frame, "文件读取中,请稍后...", in);
            int c = 0;
            byte[] bytes = new byte[1024]; // 定义byte数组
            while ((c = pm.read(bytes)) != -1) { // 循环读取文件
                fop.write(bytes, 0, c); // 通过流写数据
            }
            fop.close(); // 关闭输出流
            pm.close(); // 关闭输入流
        } catch (exception ex) {
            ex.printstacktrace();
        }
    }
}

3. 再看主窗体。jlabel、jtextfield神马的就不用说了,选择文件和选择文件夹这两个按钮也是老生常谈了,最重要的是“开始复制”按钮,将由它来负责弹出这个进度条,这就需要开辟一个新的线程,所以主窗体不仅要继承jframe类,还要实现runnable接口。他大爷的。

4. 在开始复制按钮的具体方法里,首先创建一个thread对象作为新的线程,然后调用该对象的start()方法,重载执行run()方法,在该方法中创建一个进度条对象,使用jtextfield类的gettext()方法获取要复制的文件地址和要复制到的路径,然后调用进度条类里的方法。

代码如下:

复制代码 代码如下:
package cn.edu.xidian.crytoll;
import java.awt.borderlayout;
import java.awt.desktop;
import java.awt.dimension;
import java.awt.eventqueue;
import java.awt.gridbagconstraints;
import java.awt.gridbaglayout;
import java.awt.insets;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.io.file;
import java.io.filenotfoundexception;
import java.io.filereader;
import java.io.filewriter;
import java.io.ioexception;

import javax.swing.jbutton;
import javax.swing.jfilechooser;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.joptionpane;
import javax.swing.jpanel;
import javax.swing.jtextfield;
import javax.swing.border.emptyborder;
import javax.swing.filechooser.filenameextensionfilter;

public class usermonitorframe extends jframe implements runnable{

    /**
     *
     */
    private static final long serialversionuid = 8674569541853793419l;
    private jpanel contentpane;
    private jtextfield filefield;
    private jtextfield searchtextfield;
    private jtextfield replacetextfield;
    private file file;
    private jtextfield textfield;
    private jtextfield textfield_1;

    /**
     * launch the application.
     */
    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();
                }
            }
        });
    }

    /**
     * create the frame.
     */
    public usermonitorframe() {
        setresizable(false);
        setdefaultcloseoperation(jframe.exit_on_close);
        setbounds(100, 100, 501, 184);
        settitle("在读取文件时使用进度条");
        getcontentpane().setlayout(null);

        jlabel label = new jlabel("\u6587\u4ef6\u5730\u5740\uff1a");
        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("\u9009\u62e9\u6587\u4ef6");
        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("\u590d\u5236\u5730\u5740\uff1a");
        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("\u9009\u62e9\u5730\u5740");
        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("\u5f00\u59cb\u590d\u5236");
        button_2.addactionlistener(new actionlistener() {
            public void actionperformed(actionevent e) {
                do_copybutton_actionperformed(e);
            }
        });
        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 path = textfield.gettext();
        string save = textfield_1.gettext();
        test.useprogressmonitor(this,path,save+path.substring(path.lastindexof("."),path.length()));

    }
}

《java实现在复制文件时使用进度条(java实现进度条).doc》

下载本文的Word格式文档,以方便收藏与打印。