Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向用户询问源目录和目的地;创建一个新目录_Java_File_Directory_Mkdir - Fatal编程技术网

Java 向用户询问源目录和目的地;创建一个新目录

Java 向用户询问源目录和目的地;创建一个新目录,java,file,directory,mkdir,Java,File,Directory,Mkdir,这是编程作业的一个步骤,在过去的几周里,我遇到了一些麻烦。我正在重写我的代码,试图让它工作 我正在做的第一步是:创建一个与源目录同名但位于新目标中的新目录 public static void createDir (String source, String dest) throws Exception{ //Create File objects for the source directory and new destination File sourceFile = ne

这是编程作业的一个步骤,在过去的几周里,我遇到了一些麻烦。我正在重写我的代码,试图让它工作

我正在做的第一步是:创建一个与源目录同名但位于新目标中的新目录

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();
从我的代码中可以看到,我向用户询问源目录的名称以及用户希望将此目录复制到的目标。最后,我将把整个目录(包括子目录和文件)复制到用户输入的新位置的新目录中

这是我的密码。我试图将一些东西从闪存驱动器复制到桌面,但没有成功

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

当涉及到创建新的文件对象时,我感到非常困惑,因为由于文件路径和名称的原因,我不理解语法是如何工作的。我很难理解它。我的代码错了吗?关于这一点,有没有人能给我一些指导/提示

目标是要求用户提供源目录和他希望将目录复制到的新目标。现在,我的目标是简单地在新目标中创建一个与源目标同名的新目录

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();
一步一步地完全理解它

任何帮助都将不胜感激。谢谢各位

------------------------------------------编辑#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)
它将目录卷添加到我的桌面。但是,我希望它将目录“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();

您需要做的是将源文件和目标文件转换为文件(
newfile(String)
),然后使用
newfile(File,String)
创建最终文件夹位置(记住,对于String,您只需要名称,所以使用
destFile.getName()

你能给我一个编码的例子吗?我把你说的话当作这样做的:“File newSource=new File(source);File newDest=new File(dest);”虽然这本可以解释得更好,但你的建议帮助我得到了正确的答案。我编辑了我的问题,将答案包括在内。