这是编程工作的一个步骤,过去几周我一直遇到麻烦。我正在重写我的代码,试图让它工作。
我正在做的第一步是:创建一个与源目录同名但在新目的地的新目录。
正如您在我的代码中看到的那样,我向用户询问源目录的名称以及用户希望将该目录复制到的目标。最后,我将整个目录(包括子目录和文件)复制到用户输入的新位置的新目录中。
这是我的代码。我试图将某些东西从我的闪存驱动器复制到我的桌面,但它没有用。
package recursivedirduplication;
/**
* @author zhughes3
* Last edited Tuesday, March 31st, 2014 @ 12pm
*/
import java.io.*;
import java.util.Scanner;
public class RecursiveDirDuplication {
/**
* The following program:
* 1. Asks the user for the source directory and destination.
* 2. Makes a new directory in the new location with the same name
* as the source directory.
* 3. Creates an array with File class objects for each item in the contents
* of the source directory.
* 4. Next, it iterates the array, and for each item in the array:
* - if it is a file, it copies the file to the new directory using the
* copyFile() method taken from CopyFileDemoE.
* - if it is a directory, recursively call this method to copy the
* directory and all of its contents.
*/
public static void main(String[] args) throws Exception{
// Create a new instance of Scanner to get user input
Scanner scanner = new Scanner (System.in);
//Ask user to input the directory to be copied
System.out.print("Input directory to be copied.");
//Save input as a String
String source = scanner.nextLine();
//Ask user to input destination where directory will be copied
System.out.print("Input destination where directory will be moved to.");
//Save input as String
String dest = scanner.nextLine();
//Make a new directory in the new location with the same name as the
//source directory
createDir(source,dest);
}
public static void createDir (String source, String dest) throws Exception{
//Create a File object for new directory in new location with same name
//as source directory
File newDir = new File (dest + source);
//Create a new directory in new user-inputted destination with same name
//as the source directory
newDir.mkdir();
}
正如您可以通过有关该计划的说明中的步骤所述:我现在只尝试处理第2步。
在创建新的File对象时,我感到非常困惑,因为我不了解语法如何工作,因为文件路径和名称。我很难理解它。我的代码错了吗?是否有任何指示/提示,任何人都可以给我这个。
目标是向用户询问他希望将目录复制到的源目录和新目标。现在我的目标是在新目标中创建一个与源目标同名的新目录。
一步一步地完全理解它。
非常感谢任何帮助。谢谢你们。
------------------------------------------编辑#1: - ---------------------------------------- 在查看其他Stackoverflow问题后,我将第二个方法createDir中的代码更改为:
public static void createDir (String source, String dest) throws Exception{
//Create a File object for new directory in new location with same name
//as source directory
File newDest = new File (dest);
File newDir = new File (newDest, source);
//Create a new directory in new user-inputted destination with same name
//as the source directory
newDir.mkdirs();
}
通过这个,我能够将文件从闪存驱动器写入我的桌面。 这就是我的程序输出的内容:
Input directory to be copied./Volumes/DJ BLU-Z/Letters to Palmer
Input destination where directory will be moved to./Users/Zhughes3/Desktop
BUILD SUCCESSFUL (total time: 22 seconds)
它将目录Volumes添加到我的桌面。但是,我希望它发送目录" Letters to Palmer"到我的桌面。
有人建议吗?
--------------------------------编辑#2 w /正确答案-------- -------------------------- 以下是我对程序的第二种方法所做的更改,它允许我将用户输入的目录写入新目的地。
public static void createDir (String source, String dest) throws Exception{
//Create File objects for the source directory and new destination
File sourceFile = new File (source);
File newDest = new File (dest);
//Create new File object using the File object of the new destination
//and the last name in the pathname's name sequence of the source Dir
File newDir = new File (newDest, sourceFile.getName());
//Create a new directory in new user-inputted destination with same name
//as the source directory
newDir.mkdir();
答案 0 :(得分:0)
您需要做的是将source和dest转换为文件(new File(String)
),然后使用new File(File,String)
创建最终文件夹位置(请记住,对于String,您只需要名称,因此请使用{{1 }}