Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:58:33+00:00 2026-06-09T20:58:33+00:00

Basicly, lets say I got File f1 = new File(C:\\somedir\\batch1.bat); and File f2 =

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:58:34+00:00Added an answer on June 9, 2026 at 8:58 pm

    Hmm I’m sure not the problem but doing this is more readable:

    File f = new File(filePathString);
    if(!f.exists()) { /* do something */ }
    

    rather then:

    if(f1.exists() == false)
        {
          ...
        }
    

    also when deleting a file always check its return value:

    if(f.delete()) {//deleted successfully
    }else {//couldnt delete
       //show error message
    }
    

    and as PeterLawrey said you should do the same for createNewFile():

    if(f.createNewFile()) {//created successfully
    }else {//couldnt create
       //show error message
    }
    

    and lastly always check for permissions before trying to do anything:

    if(f.canRead()&&f.canWrite()) {//can read and write free to do what is needed
       //do stuff
    }else {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have: WriteableBitmap bmp; I basicly want to save it into a file on
Scratch this! I have googled my ass off looking for this. 1. Lets say
I basicly need to load the whole XML file, add a new line with
Lets say a have program.exe , i will create a python script that will
Whats up peeps.. lets say i have a ul with 30 li items <ul
Lets say I have a post, this post should have a certian amount of
Hey still new to JS/CSS/HTML well basicly webdevelopment. I have a page, for which
I have written a very simple file managing database that basicly looks like this:
I have defined a ConfigurationProperty class, which is basicly a key-value pair (with key
I have an odd problem. Basicly my page works fine however after a small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.