Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

createNewFile( ) causes warning message, how to eliminate?

By using createNewFile method and delete method of the File class I successfully generate files from my program. But there is an annoying warning message after the compilation process. My question is how can I remove that warning messages without using @SUPPRESSWARNIGN. Because when I do inspection for my code I see a probable bug warnings which are caused by these 2 methods. Yes, by using @SuppressWarning warnings and probable bug messages go away.

I do not know if it is related with the Java version but in any case I am using Java 8. I did the research for this problem, could not find anything on the internet. I saw people on the internet used these 2 methods in the same way I used. May be they ignored the warning messages. But I do not want to.

Here is my code :

private void createAFile() throws IOException {

    String outputFileName = getFileName();
    String outputPathName = getFilePath();
    String fullOutputPath = outputPathName + "/" + outputFileName;

    output = new File(fullOutputPath);

    if(output.exists()){

        output.delete(); //this returns a boolean variable.

    }

    output.createNewFile(); //this also return a boolean variable.


}

Warnings are :

Warning:(79, 20) Result of 'File.delete()' is ignored. Warning:(84, 16) Result of 'File.createNewFile()' is ignored.

Thank you

like image 570
Kadir GUNEL Avatar asked Jun 17 '14 09:06

Kadir GUNEL


2 Answers

If you want to avoid these messages you can provide logging for the case when these method return false.

Something like this

private static Logger LOG = Logger.getLogger("myClassName");
// some code
if (!output.delete()) {
  LOG.info("Cannot delete file: " + output);
}
like image 197
Sergiy Medvynskyy Avatar answered Oct 29 '22 11:10

Sergiy Medvynskyy


These look like warnings generated from a code check tool . What i would do is this :

boolean deleted,created; // both should be instantiatd to false by default
if(output.exists()){

   deleted = output.delete(); //this returns a boolean variable.
} 
if(deleted){ 
    created = output.createNewFile();
}
if(!deleted||!created){
    // log some type of warning here or even throw an exception
}
like image 37
omu_negru Avatar answered Oct 29 '22 11:10

omu_negru