Create a directory if it does not exist and then create the files in that directory as well

前端 未结 7 1247
礼貌的吻别
礼貌的吻别 2020-12-12 14:36

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

7条回答
  •  心在旅途
    2020-12-12 15:15

    Using java.nio.Path it would be quite simple -

    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);
        }
    

提交回复
热议问题