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
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);
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With