Basicly, lets say I got File f1 = new File("C:\\somedir\\batch1.bat"); and File f2 = new File("C:\\somedir\\batch2.bat"); and I have 2 ifs
if(f1.exists() == false)
{
showMessage("File 1 not detected, creating new...");
f1.createNewFile();
}
else
{
showMessage("File 1 detected, deleting it and creating new...");
f1.delete();
f1.createNewFile();
}
and
if(f2.exists() == false)
{
showMessage("File 2 not detected, creating new...");
f2.createNewFile();
}
else
{
showMessage("File 2 detected, deleting it and creating new...");
f2.delete();
f2.createNewFile();
}
First if executes “else” code no matter if file exists or not, and second one executes “if” part, without creating new file. help please!
EDIT
My showMessage(String msg) method does System.out.println(msg) just so you know.
Hmm I’m sure not the problem but doing this is more readable:
rather then:
also when deleting a file always check its return value:
and as PeterLawrey said you should do the same for
createNewFile():and lastly always check for permissions before trying to do anything: