java 如何使用java创建一个新目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17229382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:22:58  来源:igfitidea点击:

How to create a new directory using java?

javadirectory

提问by lbear

I am using the following code to create folder but it does not create it (output is failed) and does not throw any exception.

我正在使用以下代码创建文件夹,但它没有创建它(输出失败)并且没有抛出任何异常。

Folder java is already created, I need to pass the folder name and create it in the java folder.

文件夹java已经创建,我需要传递文件夹名称并在java文件夹中创建它。

private String CreateFolder(String myfolder) {
        try {
            String dir = "../Java/" + myfolder;
            boolean result = false;
            File directory = new File(dir);

            if (!directory.exists()) {
                result = directory.mkdir();

                if (result) {
                    System.out.println("Folder is created");
                    return dir;
                } else {
                    return "failed";
                }
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return "";
    }

回答by Yakiv Mospan

Try something like this :

尝试这样的事情:

  public static void main(String[] args)
{
    String path = "E:\test";
    createFolder(path);
}

private static boolean createFolder(String theFilePath)
{
    boolean result = false;

    File directory = new File(theFilePath);

    if (directory.exists()) {
        System.out.println("Folder already exists");
    } else {
        result = directory.mkdirs();
    }

    return result;
}

Make sure to use correct root dir path (for example if you want to create folder inside of "../somefolder" it must be created already) if you want to use mkdir().

如果要使用mkdir() ,请确保使用正确的根目录路径(例如,如果要在“../somefolder”内创建文件夹,则必须已创建该文件夹

Noteyou need to set two slashes after Drive name. Like this "E:\\".

请注意,您需要在驱动器名称后设置两个斜杠。像这样"E:\\"

You can find more info here.

您可以在此处找到更多信息。

回答by user2041815

Please try giving absolute path to the directory instead of relative path.

请尝试提供目录的绝对路径而不是相对路径。

回答by bluearrow

You can try File.mkdirs() to try make nest directories and print the directory absolute path. I think you use the wrong path of "Java".

您可以尝试 File.mkdirs() 尝试创建嵌套目录并打印目录绝对路径。我认为您使用了错误的“Java”路径。

System.out.println(directory.getAbsolutePath())

回答by lbear

Please make sure the folder ../Java/exits. If there is no folder Java. The code won't work. If you really want create folder Javaautomaticlly. Please use direcotry.mkdirs()instead.

请确保文件夹../Java/退出。如果没有文件夹Java。该代码将不起作用。如果你真的想Java自动创建文件夹。请direcotry.mkdirs()改用。

回答by Sunil Kpmbl

Is this part of a web application? then use context path instead of abs path. Also use File.separator instead of slashes(/)

这是 Web 应用程序的一部分吗?然后使用上下文路径而不是 abs 路径。也使用 File.separator 而不是斜线(/)