我正在尝试使用 Apache Common 的 FileUtil 来使用 CopyDirectory 将文件传输到其他磁盘上。但是,在传输“我的文档”时出现错误。另外,我不知道为什么,但是当我选择像 C:\Users[UserName]\Documents 这样的特定路径时,它不会只给我 Documents 中的内容,而是给我用户文件夹中的其他一些文件夹。 .我不知道这是为什么。当我在 MyDocuments 上运行 CopyDirectory 时,我也会出现 IOException ......当它试图复制 MyMusic 时(我不知道它为什么首先这样做。
在我的代码中,它体面地复制了收藏夹和桌面......尽管它似乎已将收藏夹“收藏夹工具栏”中的文件夹更改为“链接”文件夹。
任何帮助都会很棒。谢谢!
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.io.*;
public class TransferJet {
public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
System.out.print("Please enter the username:");
String user = in.readLine();
System.out.print("Please enter the drive letter of the old primary partition:");
String oldPrimaryLetter = in.readLine();
System.out.print("Please enter the drive letter of the old secondary partition:");
String oldSecondaryLetter = in.readLine();
System.out.print("Please enter the drive letter of the new primary partition:");
String newPrimaryLetter = in.readLine();
System.out.print("Please enter the driver letter of the new secondary partition:");
String newSecondaryLetter = in.readLine();
if(user.equals("") || oldSecondaryLetter.equals("") || oldPrimaryLetter.equals("") || newPrimaryLetter.equals("") || newSecondaryLetter.equals(""))
{
System.out.println("Invalid directory or file name.");
System.exit(0);
}
String newFiles = "";
String oldFiles = "";
File source = null;
File destination = null;
System.out.println("Moving Favorites...");
oldFiles = oldPrimaryLetter + ":\\Users\\" + user + "\\Favorites";
//String oldFilesA = "C:\\Users\\ashih\\Favorites";
//String newFilesA = "G:\\Users\\ashih\\Favorites";
newFiles = newPrimaryLetter + ":\\Users\\" + user + "\\Favorites";
source = new File(oldFiles);
destination = new File(newFiles);
FileUtils.copyDirectory(source, destination);
System.out.println("Moving Desktop...");
oldFiles = oldPrimaryLetter + ":\\Users\\" + user + "\\Desktop";
newFiles = newPrimaryLetter + ":\\Users\\" + user + "\\Desktop";
source = new File(oldFiles);
destination = new File(newFiles);
FileUtils.copyDirectory(source, destination);
System.out.println("Moving My Documents...");
oldFiles = oldPrimaryLetter + ":\\Users\\" + user + "\\";
newFiles = newPrimaryLetter + ":\\Users\\" + user + "\\Documents";
source = new File(oldFiles);
destination = new File(newFiles);
FileUtils.copyDirectory(source, destination);
System.out.println("Moving Old Secondary to New Secondary...");
oldFiles = oldSecondaryLetter + ":\\";
newFiles = newSecondaryLetter + ":\\";
source = new File(oldFiles);
destination = new File(newFiles);
FileUtils.copyDirectory(source, destination);
}
}