Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Question about a nonexistent file

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
 
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Marshal
Posts: 79634
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79634
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic