Java 将文件夹复制到名为相同内容的目标
我正在尝试获取一个备份程序,以获取用户使用Java 将文件夹复制到名为相同内容的目标,java,file,io,copy,backup,Java,File,Io,Copy,Backup,我正在尝试获取一个备份程序,以获取用户使用JFileChooser选择的文件夹,并将其复制到同样使用相同方法选择的目标 唯一的问题是它没有将所选文件夹的所有内容放入目标中名为相同内容的文件夹中,我真的不知道为什么。我在谷歌上搜索了一下,没有发现任何有用的东西 代码如下: package main; import java.io.File; import java.util.ArrayList; public class test { BackgroundWorker bw; static
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");
}
}
}
缺少我需要的部分代码。我看不出您在哪里决定如何将源文件附加到目标路径,所以我写了这个快速示例,它说明了这一点
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);
}
File sourcePath=新文件(“/path/to/be/backed/up”);
文件destPath=新文件(“X:/backupsere”);
//从sourcePath获取所有文件
List listFiles=getFilesFrom(sourcePath);
用于(文件备份:列表文件){
//现在我们需要剥离源路径
//获取文件名
字符串文件名=toBackup.getName();
//获取父文件夹的路径
字符串路径=toBackup.getParent();
//从文件路径中删除源路径
path=path.substring(sourcePath.getPath().length());
//将文件名附加到路径
path=path+File.separator+fileName;
//现在我们有了备份文件的名称
字符串backupFile=destPath+path;
System.out.println(“备份到”+备份文件);
}
基本上,您需要去掉“源路径”(要复制的目录)。然后使用结果值附加到“备份路径”值,然后应该有一个合适的路径。确实需要查看确定
dir
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);
}