abhishek ringsia
abhishek ringsia

Reputation: 2050

Check if parent directory exists or not in java

How do I check whether a parent directory is created or not before creating file in that directory? I can use file.exists() but it only checks if my file obj is created and not my parent directory/parent path.

Upvotes: 1

Views: 3237

Answers (2)

Siboo
Siboo

Reputation: 61

public boolean exists();

that will Tests whether the file or directory denoted by this abstract pathname exists. .

String path = "URL";
File f = new File(path);
f.exists();
f.getParentFile().mkdirs(); //File with Create Dir
f.createNewFile();

Upvotes: 1

Alexandre Beaudet
Alexandre Beaudet

Reputation: 2844

Try that :

 file.getParentFile().exists();

Will return true if the parent exists, false if not.

Upvotes: 1

Related Questions