如何使用java代码在特定路径创建文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23955836/
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-08-14 09:23:41  来源:igfitidea点击:

how to create folder at perticular path using java code

javafile-io

提问by user3691407

I want to create a folder at a particular path (location which is given by user) using java code.

我想使用 java 代码在特定路径(由用户提供的位置)创建一个文件夹。

File dir = new File("Hermatic");
dir.mkdir();

This code create 'hermatic' folder but i want to set path so that folder create at given location.

此代码创建“hermatic”文件夹,但我想设置路径,以便在给定位置创建该文件夹。

回答by Gabriel Ruiu

Is this what you are looking for?

这是你想要的?

public class Main {
    public static void createFolder(String path) throws Exception {
        File dir = new File(path);
        dir.mkdir();
    }

    public static void main(String[] args) throws Exception {
        String path = args[0]; //takes the first argument from the command line
        createFolder(path);
    }
}

So running your application with

所以运行你的应用程序

java -jar myapp.jar /home/me/folder

would create the folder with the path /home/me/folder

将创建路径为/home/me/folder 的文件夹

回答by yanyu

String location = "C:\user\Desktop\dir1\dir2\";
File file = new File(location + "filename.txt");