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

后端 未结 9 2433
感动是毒
感动是毒 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:05
    //Create New File if not present
    if (!file.exists()) {
        file.getParentFile().mkdirs();
    
        file.createNewFile();
        Log.e(TAG, "File Created");
    }
    
    0 讨论(0)
  • 2020-12-15 04:06

    I think the exception you get is likely the result from the file check of the atomic method file.createNewFile(). The method can't check if the file does exist because some of the parent directories do not exist or you have no permissions to access them. I would suggest this:

    if (file.getParentFile() != null && !file.getParentFile().mkDirs()) {
        // handle permission problems here
    }
    // either no parent directories there or we have created missing directories
    if (file.createNewFile() || file.isFile()) {
        // ready to write your content
    } else {
        // handle directory here
    }
    

    If you take concurrency into account, all these checks are useless because in every case some other thread is able to create, delete or do anything else with your file. In this case you have to use file locks which I would not suggest doing ;)

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-12-15 04:10

    Perhaps the directory the file is being created in doesn't exist?

    0 讨论(0)
  • 2020-12-15 04:15

    According to the [java docs](http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#createNewFile() ) createNewFile will create a new file atomically for you.

    Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

    Given that createNewFile is atomic and won't over-write an existing file you can re-write your code as

    try {
        if(!file.createNewFile()) {
            System.out.println("File already exists");
        } 
    } catch (IOException ex) {
        System.out.println(ex);
    }
    

    This may make any potential threading issues, race-conditions, etc, easier to spot.

    0 讨论(0)
  • 2020-12-15 04:17

    normally this is something you changed recently, first off your sample code is if not file exists and not create new file - you are trying to code away something - what is it?

    Then, look at a directory listing to see if it actually exists and do a println / toString() on the file object and getMessage() on the exception, as well as print stack trace.

    Then, start from zero knowledge again and re factor from the get-go each step you are using to get here. It's probably a duh you stuck in there somewhere while conceptualizing in code ( because it was working ) - you just retrace each step in detail, you will find it.

    0 讨论(0)
提交回复
热议问题