gpt4 book ai didi

java - 将文件夹复制到具有相同名称的目标

转载 作者:行者123 更新时间:2023-12-01 15:12:27 25 4
gpt4 key购买 nike

我正在尝试让备份程序获取用户使用JFileChooser选择的文件夹,并将其复制到也使用相同方法选择的目标。

唯一的问题是,它不会将所选文件夹的所有内容放入目标中命名相同的文件夹中,我真的不知道为什么。我做了一些谷歌搜索,但没有发现任何有用的东西。

代码如下:

package main;

import java.io.File;
import java.util.ArrayList;

public class test {

BackgroundWorker bw;
static ArrayList bgWorker = new ArrayList();
ArrayList al = new ArrayList(); // this is the list of files selected to
// back up
String dir = ""; // this is the path to back everything up to selected by
static // the user
boolean bwInitiallized = false;

public void startBackup() throws Exception {
Panel.txtArea.append("Starting Backup...\n");

for (int i = 0; i < al.size(); i++) {
/**
* THIS IS WHERE I NEED TO CREATE THE FOLDER THAT EACH BACKUP FILE
* WILL GO INTO EX: SC2 GOES INTO A FOLDER CALLED SC2 AND RIOT GOES
* TO RIOT, ALL WITHIN THE DIRECTORY CHOSEN
*/
File file = new File((String) al.get(i));
File directory = new File(dir);

// File dirFile = new File(dir + "\\" + file.getName());
// if (!dirFile.exists())
// dirFile.mkdir();

bw = new BackgroundWorker(Panel.txtArea, file, directory);
bgWorker.add(bw);
bwInitiallized = true;
bw.execute();

/**
* follows to the bottom of the txtarea
*/
int x;
Panel.txtArea.selectAll();
x = Panel.txtArea.getSelectionEnd();
Panel.txtArea.select(1, x);

}
clearList(); // method not included in this example that deletes all the
// contents of the al array list.
}

public static void cancel() {
BackgroundWorker bg;
if (bwInitiallized) {
bwInitiallized = false;
Panel.txtArea.append("Cancelling...\n");
for (int i = 0; i < bgWorker.size(); i++) {
// BackgroundWorker bg = (BackgroundWorker) bgWorker.get(i);
bg = (BackgroundWorker) bgWorker.get(i);
bg.cancel(true);
}
Panel.txtArea.append("Canceled backUp!\n");
} else {
Panel.txtArea.append("Cannot Cancel! Not Initiallized!\n");
}
}
}

我认为问题是:我相信无论出于何种原因,目标文件路径都需要包含包含的文件夹的名称,但我尝试过,但没有帮助。

有人知道我做错了什么吗?

这是生成JFileChooser的代码:

public void fileChooserToDestination() {
LookAndFeel previousLF = UIManager.getLookAndFeel();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
JFileChooser jfc = new JFileChooser();
try {
UIManager.setLookAndFeel(previousLF);
} catch (UnsupportedLookAndFeelException e) {
}

jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

if (jfc.showDialog(null, "Select Directory") == JFileChooser.APPROVE_OPTION) {
File file = jfc.getSelectedFile();
dir = file.getPath();
Panel.txtArea.append("User selected " + file.getPath()
+ " for the destination...\n");
try {
startBackup();
} catch (Exception e) {
}

} else {
Dialogs.msg("You canceled selecting a destination folder! Returning to main screen...");
al.clear();
Panel.txtArea.append("User cancelled the destination selection..."
+ "\n");
}

return;
}

最佳答案

我需要的部分代码丢失了。我看不到您在哪里决定如何将源文件附加到目标路径,因此我编写了这个快速示例来说明这一点...

File sourcePath = new File("/path/to/be/backed/up");
File destPath = new File("X:/BackupHere");

// Get all the files from sourcePath
List<File> listFiles = getFilesFrom(sourcePath);

for (File toBackup : listFiles) {

// Now we need to strip off the sourcePath
// Get the name of the file
String fileName = toBackup.getName();
// Get parent folder's path
String path = toBackup.getParent();
// Remove the source path from file path
path = path.substring(sourcePath.getPath().length());

// Append the file name to the path
path = path + File.separator + fileName;

// Now we have the name of the back up file
String backupFile = destPath + path;

System.out.println("Backup to " + backupFile);

}

基本上,您需要删除“源路径”(您要复制的目录)。然后,您可以使用结果值附加到“备份路径”值,然后您应该有一个合适的路径。

关于java - 将文件夹复制到具有相同名称的目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12151940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com