我相信这个问题以前已经被问过了,但是我找到的答案都不能很好地与我的现有代码配合使用。如果有一种方法可以在不完全重做我已有的代码的情况下完成它,我会发表这个问题。
这个想法是在从一个驱动器复制文件和目录的同时显示一个非常基本的进度条。我有一个名为BasicCopy的类,旨在将Pictures、Documents、videos和Music文件夹(标准的Windows机器上)的内容复制到第二个驱动器上的同名文件夹的备份目录中。以下是目前为止的类:
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
public class BasicCopy {
String username = "";
String mainDrive = "";
String backupDrive = "";
String backupDir = "";
String[] directories;
public BasicCopy(String inDrive, String outDrive, String username){
mainDrive = inDrive;
backupDrive = outDrive;
this.username = username;
createBackupDirectory();
copyDirectories();
close();
}
//Create backup directory
public void createBackupDirectory(){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy_HMMSS");
String timestamp = sdf.format(date);
backupDir = backupDrive + ":\\" + "Backup " + timestamp;
File backupDirectory = new File(backupDir);
backupDirectory.mkdir();
}
public void copyDirectories(){
//Main directories
String pics = mainDrive + ":\\Users\\" + username + "\\Pictures";
String docs = mainDrive + ":\\Users\\" + username + "\\Documents";
String vids = mainDrive + ":\\Users\\" + username + "\\Videos";
String musc = mainDrive + ":\\Users\\" + username + "\\Music";
//Backup directories
String bkPics = backupDir + "\\Pictures";
String bkDocs = backupDir + "\\Documents";
String bkVids = backupDir + "\\Documents";
String bkMusc = backupDir + "\\Pictures";
String[] directories = {pics, docs, vids, musc};
String[] bkDirectories = {bkPics, bkDocs, bkVids, bkMusc};
//Loop through directories and copy files
for (int i = 0; i < directories.length; i++){
File src = new File(directories[i]);
File dest = new File(bkDirectories[i]);
try{
FileUtils.copyDirectory(src, dest);
} catch (IOException e){
e.printStackTrace();
}
}
}
/* Close current process */
public void close(){
System.exit(0);
}
}
我之前有一个类中的方法,可以测量目录的总大小,因此如果需要的话,我可以将其传递给这个类。但是,目前我仅循环遍历四个目录,因此我预计在每个刻度上无法以高于25%的分辨率增加进度条。我在想是否有办法更改它,以便可以包含一个进度条来监视它,并且让它更准确一些?此外,我不确定是否应在不同的线程中提出此问题,但是这种文件复制方法需要非常长的时间。复制500MB的文件需要数小时,我想知道是否有方法可以加快速度?虽然那部分不是优先考虑的,但现在我主要是想添加一个进度条。干杯!
编辑:
经过一些试验,我意识到我可能可以使用类似于以下代码的代码(该代码可能有效,也可能无效 - 我只是快速写下来以免忘记,它仍未经测试)。这将允许我为每个复制的文件更新进度条。
for (int i = 0; i < directories.length; i++){
File dir = new File(directories[i]);
File dest = new File(bkDirectories[i]);
for(File file: dir.listFiles()){
try{
FileUtils.copyDirectory(file, dest);
//update progress bar here
} catch (IOException e){
e.printStackTrace();
}
}
}
编辑 #2:
我已经在代码上做了更多的工作,我相信大部分问题都解决了。现在的问题是关于SwingWorker,我相信我需要它来在后台运行长期的方法。否则GUI会变得不响应(Java文档中有很多关于这方面的文档)。然而,这就是我被卡住的地方。我之前只用过一次SwingWorker,而且那主要是复制了别人的代码。我在想如何使用下面的代码来实现SwingWorker,以便进度条(以及整个框架)能够真正出现。
更新的代码:
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JProgressBar;
import javax.swing.JLabel;
import org.apache.commons.io.FileUtils;
@SuppressWarnings("serial")
public class BasicCopy extends JFrame {
private JPanel contentPane;
private JTextArea txtCopiedDirs;
private JButton btnCancel;
private JProgressBar progressBar;
private JLabel lblCopying;
private String mainDrive;
private String backupDrive;
private String username;
private String backupDir;
long totalSize = 0; //total size of directories/files
long currentSize = 0; //current size of files counting up to ONE_PERCENT
int currentPercent = 0; //current progress in %
long ONE_PERCENT; //totalSize / 100
public BasicCopy() {
}
public BasicCopy(String inDrive, String outDrive, String username, long space){
mainDrive = inDrive;
backupDrive = outDrive;
this.username = username;
totalSize = space;
ONE_PERCENT = totalSize/100;
createGUI();
/* Should not have these here!
* Pretty sure I need a SwingWorker
*/
createBackupDirectory();
copyDirectories();
}
public void createGUI(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Backup Progress");
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
txtCopiedDirs = new JTextArea();
txtCopiedDirs.setBounds(10, 56, 414, 125);
contentPane.add(txtCopiedDirs);
btnCancel = new JButton("Cancel");
btnCancel.setBounds(169, 227, 89, 23);
contentPane.add(btnCancel);
progressBar = new JProgressBar(0, 100);
progressBar.setBounds(10, 192, 414, 24);
progressBar.setValue(0);
contentPane.add(progressBar);
lblCopying = new JLabel("Now backing up your files....");
lblCopying.setBounds(10, 11, 157, 34);
contentPane.add(lblCopying);
setVisible(true);
}
//Create backup directory
public void createBackupDirectory(){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy_HMMSS");
String timestamp = sdf.format(date);
backupDir = backupDrive + ":\\" + "Backup " + timestamp;
File backupDirectory = new File(backupDir);
backupDirectory.mkdir();
}
public void copyDirectories(){
//Main directories
String pics = mainDrive + ":\\Users\\" + username + "\\Pictures";
String docs = mainDrive + ":\\Users\\" + username + "\\Documents";
String vids = mainDrive + ":\\Users\\" + username + "\\Videos";
String musc = mainDrive + ":\\Users\\" + username + "\\Music";
//Backup directories
String bkPics = backupDir + "\\Pictures";
String bkDocs = backupDir + "\\Documents";
String bkVids = backupDir + "\\Documents";
String bkMusc = backupDir + "\\Pictures";
String[] directories = {pics, docs, vids, musc};
String[] bkDirectories = {bkPics, bkDocs, bkVids, bkMusc};
//Loop through directories and copy files
for (int i = 0; i < directories.length; i++){
File dir = new File(directories[i]);
File dest = new File(bkDirectories[i]);
for(File file: dir.listFiles()){
try{
FileUtils.copyDirectory(file, dest);
if(getDirSize(file) >= ONE_PERCENT){
currentPercent++;
progressBar.setValue(currentPercent);
currentSize = 0;
} else {
currentSize = currentSize + getDirSize(file);
if(currentSize >= ONE_PERCENT){
currentPercent++;
progressBar.setValue(currentPercent);
currentSize = 0;
}
}
} catch (IOException e){
e.printStackTrace();
}
}
}
}
public static Long getDirSize(File directory) {
long size = 0L;
if (directory.listFiles() != null){
for (File file : directory.listFiles()) {
size += file.isDirectory() ? getDirSize(file) : file.length();
}
}
return size;
}
/* Close current window */
public void closeWindow() {
WindowEvent close = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(close);
System.exit(0);
}
}
btnCancel.setBounds(169, 227, 89, 23);
你从哪里获取代码示例的?总之,这是非常糟糕的代码,不应该这样做。我想知道是什么资源导致人们走上了这条错误的道路。Thread.sleep(n)
,而是实现Swing定时器进行重复任务或SwingWorker进行长时间运行的任务。有关更多详细信息,请参见Swing中的并发性。