CreateNewFile () causes a warning, how to fix it?

Using the createNewFile method and the delete method of the File class, I successfully create files from my program. But after the compilation process, an annoying warning message appears. My question is how to remove these warning messages without using @SUPPRESSWARNIGN . Because when I check my code, I see likely error warnings caused by these two methods. Yes, when using @SuppressWarning warnings and likely error messages go away.

I don’t know if this is related to the Java version, but in any case I use Java 8. I did research on this issue, could not find anything on the Internet. I saw how people on the Internet used these 2 methods in the same way as I used. Maybe they ignored the warning messages. But I dont want.

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:

Warning: (79, 20) The result of "File.delete ()" is ignored. Warning: (84, 16) The result "File.createNewFile ()" is ignored.

thanks

+6
source share
2 answers

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

Something like that

 private static Logger LOG = Logger.getLogger("myClassName"); // some code if (!output.delete()) { LOG.info("Cannot delete file: " + output); } 
+8
source

They look like warnings generated by a code verification tool. I would do 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 } 
+3
source

Source: https://habr.com/ru/post/970939/


All Articles