Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

java - How create progress bar while file transferring

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I can think of two ways.

Swing Worker

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.

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

This solution will require you to supply you own UI

Progress Monitor

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

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

(Example stolen from Java Docs)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...