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

前端 未结 7 1246
礼貌的吻别
礼貌的吻别 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:23

    Trying to make this as short and simple as possible. Creates directory if it doesn't exist, and then returns the desired file:

    /** Creates parent directories if necessary. Then returns file */
    private static File fileWithDirectoryAssurance(String directory, String filename) {
        File dir = new File(directory);
        if (!dir.exists()) dir.mkdirs();
        return new File(directory + "/" + filename);
    }
    

提交回复
热议问题