WARNING: File.mkdir() is ignored

后端 未结 4 1769
忘了有多久
忘了有多久 2021-01-04 10:49

I\'ve created a basic Audio Recording App using Media Recorder API\'s. I\'m trying to create a folder in the SD card and to save my recorded files to that folder. I\'m getti

相关标签:
4条回答
  • 2021-01-04 11:28

    If you want to ignore this warning, add this on the method: @SuppressWarnings("all")

    0 讨论(0)
  • 2021-01-04 11:32

    I created a utility function:

    @SuppressWarnings("unused")
    private static void IGNORE_RESULT(boolean b) {}
    

    And then to use:

    IGNORE_RESULT(path.mkdir());
    

    I like this solution because it is self documenting. You are explicitly stating that you know there is a result and you are intentionally ignoring it. It should also optimize out to a NOP during compilation so there is no unnecessary test at runtime just to suppress a useless warning.

    0 讨论(0)
  • 2021-01-04 11:38

    Correct me if I'm wrong, but I expect that the (compilation) warning message really says this:

    Result of File.mkdir() is ignored

    ... or something like that. It is telling you that you are ignoring the result of the mkdir() call that tells you whether or not a directory was created.

    One way to avoid the warning would be to test the result and act appropriately. Another would be to simply assign the result to a temporary variable, ignore it, and (potentially) crash later because the directory wasn't created when it should have been.

    (Guess which solution is better ...)


    Feel free to modify the code if there is any other mistake.

    Since you asked ... it is BAD STYLE to use Hungarian notation for Java variable names. Java is a strongly typed language where all variables have a clear declared types. You should not need the mental crutches of some ghastly identifier convention to tell you what a variable's type is intended to be.

    0 讨论(0)
  • 2021-01-04 11:48

    As @Stephen C suggests, i handled in these ways

    1)

    boolean isDirectoryCreated= path.mkdir();

    and ignore 'isDirectoryCreated'

    2) (Recommended)

     boolean isDirectoryCreated=path.exists();
     if (!isDirectoryCreated) {
         isDirectoryCreated= path.mkdir();
     }
     if(isDirectoryCreated) {
        // do something
     }
    

    3)

    if (!path.exists()) {
       if(path.mkdir()){
         // do something
       }
    }
    
    0 讨论(0)
提交回复
热议问题