[groovy] How to check if a directory containing a file exist?

I am using groovy to create a file like "../A/B/file.txt". To do this, I have created a service and pass the file path to be created as an argument. This service is then used by a Job. The Job will do the logic in creating the file in the specified directory. I have manually created the "A" directory.

How will I create the "B" directory and the file.txt inside the "A" directory through codes to create it automatically?

I need also to check if directories "B" and "A" exists before creating the file.

This question is related to groovy

The answer is


EDIT: as of Java8 you'd better use Files class:

Path resultingPath = Files.createDirectories('A/B');

I don't know if this ultimately fixes your problem but class File has method mkdirs() which fully creates the path specified by the file.

File f = new File("/A/B/");
f.mkdirs();