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);
}
}
经过一番摆弄,我意识到我可能可以使用类似的代码(这段代码可能工作,也可能不工作--我只是快速地记下它,这样我就不会忘记它,它还没有经过测试)。这将允许我更新每个复制文件的进度条。
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,而且主要是使用复制的代码。我想知道如何使用下面的代码来实现它,从而使进度条(和框架的其余部分)实际出现。
更新的代码:
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);
}
}
class Task extends SwingWorker<Void, Void>{
@Override
public Void doInBackground(){
setProgress(0);
//Create Backup Directory
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();
//Copy Files
//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{
if(file.isFile()){
FileUtils.copyFileToDirectory(file, dest);
txtCopiedDirs.append(file.getAbsolutePath() + "\n");
} else if (file.isDirectory()){
FileUtils.copyDirectoryToDirectory(file, dest);
txtCopiedDirs.append(file.getAbsolutePath() + "\n");
}
if(getDirSize(file) >= ONE_PERCENT){
currentPercent = getDirSize(file)/ONE_PERCENT;
progressBar.setValue((int)currentPercent);
currentSize = 0;
} else {
currentSize = currentSize + getDirSize(file);
if(currentSize >= ONE_PERCENT){
currentPercent = currentSize/ONE_PERCENT;
currentPercent++;
progressBar.setValue((int)currentPercent);
currentSize = 0;
}
}
} catch (IOException e){
e.printStackTrace();
}
}
}
return null;
}
@Override
public void done(){
closeWindow();
}
}
该类包含在主类中,并使用以下代码启动:
Task task = new Task();
task.execute();
现在,正如我提到的,这仍然是不完美的。例如,在复制之前检查它是一个文件还是一个目录应该替换为递归函数,这样它就可以循环遍历每个单独的文件,而不是仅仅复制一个完整的目录。这将允许更准确地更新进度条,并将在文本区域中显示正在复制的单个文件,而不是文件和目录。
无论如何,我只是想发布这段代码来显示哪些东西对我有效,而不是完全重做我已有的东西。我将继续努力,但并张贴任何重要的更新,可能有助于未来的读者。
我想知道是否有办法更新使用NIO库文件的进度条。复制,用于下载工作线程中的文件。代码如下。 有一个进度条,我已经尝试绑定到任务,但似乎所做的只是导致一个错误,并立即重新启用下载按钮。
问题内容: 使用复制大型文件时,您没有任何指示操作进展的信息。 我已经把一些作品- 它使用一个简单的进度条类(简单的返回一个简单的ASCII进度栏,作为一个字符串),和一个环,并做实际的复制。它显示进度条,使用它有点黑,但它可以工作。 您可以在github上查看代码(在上下文中) 是否有任何内置模块可以更好地做到这一点?可以对此代码进行任何改进吗? 问题答案: 两件事情: 我想使默认的块大小, 很
我正在尝试使用Java复制文件。我有一个需要复制的文件对象的arraylist,但是当实际复制发生时,目标文件夹会变成一个文件,不会复制任何东西
问题内容: 我有一个较长的无符号整数文件(每个64位,0.47GB文件),需要读取并存储在数组中。经过一番思考之后,由于Java中的所有内容都已签名(请纠正我,请纠正我),因此我长时间使用了该类型,我想不出更好的选择了。无论如何,仅需对数组进行排序,因此原始数字的精确值并不是最重要的。我们应该测量排序算法的效率,仅此而已。但是,当我实际要读取文件时(在下面的代码中),我遇到了砖墙。 它会一直持续下
使用Java8。 为了获得最佳性能,我尝试用复制文件,但很快发现它不支持汉字。例如: 代码打算从jar复制一个文件,但它抛出了一个异常(a“我的" 文件夹已提前创建): 问题是鎴戠殑" 甚至连一个中国人都看不懂,所以我正在寻找一个解决办法来处理汉字。 我也尝试了FileChannel,但失败了,意识到它用于直接文件,而不是在一个jar中的文件。我该怎么做?
问题内容: 请告诉我最好/最快的方法: 1)将很小的二进制文件加载到内存中。例如图标; 2)加载/读取大小为512Mb +的非常大的二进制文件。也许我必须使用内存映射的IO? 3)当您不想考虑大小/速度而只能做的事情时,通常选择:将所有字节读入内存? 谢谢!!! 问题答案: 对于内存映射文件,java有一个nio包:Memory Mapped Files 检出小文件的字节流类:字节流 检出较大文件