• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about a nonexistent file  RSS feed

 
Alissa Horner
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code in my application. If the file does not exist, how would I get my application to continue its execution? My application stops executing if the file does not exist.

try
{
File file = new File("C:\\myfile.txt");
if (file.exists() == true)
{
file.delete();
}
}

catch(Exception e)
{
System.err.println(e.getMessage());
}
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is ther emore code? IF the file doesn't exist, the if statement doesn't run, the catch block doesn't catch anything and execution resumes after the catch. (You can add a println to verify)
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a matter of style, you should not write

file.exists() is already a boolean and will either be true or false so all you need to do is this:
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is more than a matter of style; the == true bit is error‑prone. If you write = by mistake at least in this instance the code will fail to compile.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, when a variable is involved, it's possible to introduce a bug by mistakenly using = instead of ==, as in:

This is where you can get in trouble because on Line 4, instead of comparing the variable exists to true, it is instead assigned the value true and that becomes the value of the conditional expression for the if-statement. Basically, this particular bug will cause the body of the if-statement to always execute and the else part to never be executed, even if exists is false before Line 4 is executed. Line 11 eliminates the bug. However, if you make the same mistake and use = instead of == in this code:

you will know right away that something is wrong because this code won't compile because you can't assign a value to a method call. In either case, for consistency just don't compare a boolean expression with true or false.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:True, when a variable is involved, it's possible to introduce a bug by mistakenly using = instead of == . . .
I think you get two bugs for the price of one:
  • 1: Incorrect value for the boolean variable.
  • 2: Incorrect behaviour of the if statement.
  • You can go one better with a loop; you can have a loop which never runs or an infinite loop, depending on the value after the = sign.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!