File.createNewFile() thowing IOException No such file or directory

后端 未结 9 2459
感动是毒
感动是毒 2020-12-15 03:51

I have a method that writes to a log file. If the file exists it should append to it, if not then I want it to create a new file.

if (!file.exists() &&am         


        
9条回答
  •  独厮守ぢ
    2020-12-15 04:07

    This could be a threading issue (checking and creating together are not atomic: !file.exists() && !file.createNewFile()) or the "file" is already a directory.

    Try (file.isFile()) :

    if (file.exists() && !file.isFile()){
       //handle directory is there
    }else if(!file.createNewFile()) {
       //as before
    }
    

提交回复
热议问题