java - 询问用户源目录和目的地;建立一个新目录

标签 java file directory 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 步。

在创建新的 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 带正确答案 -------- -------------------------- 这是我对程序的第二种方法所做的更改,它允许我将用户输入的目录写入新的目标。

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();

最佳答案

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

关于java - 询问用户源目录和目的地;建立一个新目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22767877/

相关文章:

java - 不应在返回参数中使用通用通配符类型

java - 如何从 startActivityForResult 读取结果

java - 通过 webdav 在基于 java 的 Web 应用程序中编辑文件(读取/编辑/保存)

linux - root创建的所有文件都被写保护

python - 在 python 中,如何将文件复制到目录中并在该目录达到一定大小时停止

python - 哪些脚本会进入 Python 包的 bin 文件夹?

java - GET 提交而不是 POST 提交。 Tapestry

java - 在运行时交换正在运行的 jar

javascript - 为什么 JavaScript 缺少导入子文件的命令?

PHP - 从数据库中显示损坏的图像