Condition is if directory exists it has to create files in that specific directory with out creating a new directory.
The below code only creating a file with new di
Using java.nio.Path it would be quite simple -
java.nio.Path
public static Path createFileWithDir(String directory, String filename) { File dir = new File(directory); if (!dir.exists()) dir.mkdirs(); return Paths.get(directory + File.separatorChar + filename); }