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

时间:2014-05-30 13:31:16

标签: java file-io

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

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

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

2 个答案:

答案 0 :(得分:1)

这是你在找什么?

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

使用

运行您的应用程序
java -jar myapp.jar /home/me/folder

将创建路径为 / home / me / folder

的文件夹

答案 1 :(得分:0)

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