Java autogenerate directories if exists

前端 未结 2 434
[愿得一人]
[愿得一人] 2021-01-28 08:38

I\'m trying to check if a directory exists, if that happens I want to give it another number, for example, if \"folderX\" exists I want to create a new one called \"FolderX1\",

2条回答
  •  囚心锁ツ
    2021-01-28 09:25

    String folderPrefix = "folder";
    int folderSuffix = 0;
    File fa = new File(folderPrefix + folderSuffix);
    
    while (fa.exists()) {
      fa = new File(folderPrefix + folderSuffix++);
    }
    

提交回复
热议问题