文件传输时如何创建进度条 [英] How create progress bar while file transferring

查看:74
本文介绍了文件传输时如何创建进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.awt.Component;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JOptionPane;
import javax.swing.ProgressMonitorInputStream;

public class buckUpFile {
    private Component parentComponent;

    public void copyFile() {
        File srcFolder = new File(
                "C:\\Users\\ALLEN\\Workspace\\FINAL_LCTP_WORKBENCE_1.5");
        File destFolder = new File(
                "C:\\Data Programing\\COPY_OF_FINAL_LCTP_WORKBENCE_1.5");

        if (!srcFolder.exists()) {
            JOptionPane.showMessageDialog(null, "Directory does not exist.");
            System.exit(0);
        } else {
            try {
                copyFolder(srcFolder, destFolder);
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(0);
            }
        }

        JOptionPane.showMessageDialog(null,
                "Back up request has been completed");
    }

    public void copyFolder(File src, File dest) throws IOException {
        if (src.isDirectory()) {
            if (!dest.exists()) {
                dest.mkdir();
            }

            String files[] = src.list();

            for (String file : files) {
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);
                copyFolder(srcFile, destFile);
            }
        } else {
            InputStream in = new BufferedInputStream(
                    new ProgressMonitorInputStream(parentComponent, "Reading "
                            + src, new FileInputStream(src)));

            OutputStream out = new FileOutputStream(dest);

            byte[] buffer = new byte[1024];

            int length;
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }

            in.close();
            out.close();
        }
    }
}

我上面的代码工作得很好,它使我可以将文件的数据从一个目录复制到另一个目录.我的问题是,如何创建进度栏?我可以附加我的代码以使我的程序更加用户友好.我尝试使用ProgressMonitorInputStream,但看来我走错了路.

The codes i have above works just fine it allows me to copy the data of a file from one directory to another. My problem is, how can i create a progress bar? that i could attach to my codes to make my program more user friendly. I tried using ProgressMonitorInputStream but it looks like I'm in the wrong path.

推荐答案

我可以想到两种方式.

首先,将代码复制包装到 SwingWorker ,使用 setProgress 方法更新进度,并使用属性更改侦听器监视对progress属性的更改.

Start by wrapping you copy code into a SwingWorker, using the setProgress method to update the progress and a property change listener to monitor changes to the progress property.

当progress属性更改时,您将随后更新UI.

When the progress property changes, you would then update the UI.

此解决方案将要求您提供自己的UI

This solution will require you to supply you own UI

使用 ProgressMonitorInputStream 拥有自己的用户界面.

Use a ProgressMonitorInputStream, which comes with it's own UI.

InputStream in = new BufferedInputStream(
    new ProgressMonitorInputStream(
        parentComponent,
        "Reading " + fileName,
        new FileInputStream(fileName)));

(示例从Java Docs被盗)

(Example stolen from Java Docs)

这篇关于文件传输时如何创建进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆