-3

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

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.

Rudi Kershaw
  • 11,535
  • 7
  • 50
  • 74
user3691407
  • 11
  • 1
  • 1
  • 1

2 Answers2

1

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

Gabriel Ruiu
  • 2,673
  • 2
  • 16
  • 22
0
String location = "C:\\user\\Desktop\\dir1\\dir2\\";
File file = new File(location + "filename.txt");
yanyu
  • 219
  • 2
  • 11