Create files with similar names using Java without overwriting existing file

后端 未结 4 824
忘了有多久
忘了有多久 2021-01-03 05:43

I was wondering if it is possible to create multiple files with similar names, without overwriting the current file.

for example: if I have a file: xyz.txt next tim

4条回答
  •  星月不相逢
    2021-01-03 05:58

    Not in java as already indicated by @Greg Kopff. But you can work around something like this:

      int count = 0;
    
      public void createFile(String name) throws IOException
      {
        File f;
        f = new File(name);
        if (!f.exists())
        {
          f.createNewFile();
        }
        else
        {
          count++;
          createFile(name + (count));
        }
      }
    

提交回复
热议问题