-3

I am trying to create folder in /Users/Documents. I am using following code:

new File("/Users/Documents/myfolder");

But it is creating /Users/Documents/myfolder in my working directory.

How can I solve this issue?

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Sudip Saha
  • 91
  • 1
  • 1
  • 9
  • 1
    I don't believe you. Also, instantiating a `File` does not create any directories. – Sotirios Delimanolis Sep 21 '15 at 03:43
  • 1
    @sudip saha use full path or read this question http://stackoverflow.com/questions/9677692/getting-my-documents-path-in-java – Madhawa Priyashantha Sep 21 '15 at 03:45
  • 2
    Please provide [minimal, complete, and verifiable](http://stackoverflow.com/help/mcve) code. – Ghazanfar Sep 21 '15 at 03:45
  • You can't make a folder by new File(). You need to do: .mkdirs(); at the end of the new File() – DarkHorse Sep 21 '15 at 03:46
  • `new File(System.getProperty("user.home"), "Documents/My Brand new Folder").mkdirs()` ... but I'd also look at the return value to make sure it worked, something like [this](http://stackoverflow.com/questions/21059085/how-can-i-create-a-file-in-the-current-users-home-directory-using-java/21059316#21059316) for example – MadProgrammer Sep 21 '15 at 03:48

2 Answers2

1

You need to use mkdir or mkdirs method from File to create directory.

File directory = new File("/Users/Documents/myfolder");
if (!directory.exists()) {
   directory.mkdirs();
}
Rodrigo Gomes
  • 346
  • 1
  • 5
1

Thanks Everyone. Using Path instance solved this problem.

Sudip Saha
  • 91
  • 1
  • 1
  • 9