Results 1 to 8 of 8
  1. #1
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default write file if it doesn't exist, but if it does exist do nothing?

    Recently I had working code which allowed me to write specific information to a file.

    When I went about changing it to an "if-else" statement It stopped being able to write. However what I've been trying to do lately is to tell Java to write a file only if it DOESN'T exist -- but if it DOES exist to leave the file alone.

    As of current I can only make it write or not write depending on how I have the statement set up. I checked out stackoverflow and I used some of their stuff, however it didn't do much. I can't figure out how to get Java to only write a file if I tell it to in an "if" statement. But otherwise do nothing.
    Java Code:
     if(happyFile.exists() ==! true){
                    happyFile.createNewFile();
                File happyFile = new File("z:\\afolder\\"+stuff+".csv");
                FileWriter fileWriter = new FileWriter(happyFile, true);
                BufferedWriter buffer = new BufferedWriter(fileWriter);
                PrintWriter printWriter = new PrintWriter(fileWriter);
                printWriter.printf( "%s\n,%s,,%s,,,,%s\n,%s,,%s,%s,%s,,%s,%s,%s,%s,%s,,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",stringthing,name1,name2,name3,Serial,part,assembly,anode,cathode,header,tantalum,Cap,ESR,Leakage,Assemblylotselected,Anode1,Anode2,Anode3,Anode4,Anode5,Cathodelot,TeflonSpacer1,TeflonSpacer2,TeflonSpacer3,HeaderGlass,Tantalumlot,Teflon);
                printWriter.close();         
        
                                             }           
                else{
                     
                    System.out.println("else");
                 }
    For some reason I can't figure out this won't write the file if happyfile does not exist. Even if I put "false" Which I am assuming is equivalent to "not true" or ==! true I'm not quite sure what to do here as I've tried all that I know.
    Last edited by innovo; 08-12-2015 at 08:00 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: write file if it doesn't exist, but if it does exist do nothing?

    Your syntax threw me for a minute. But it should have worked. You need to ensure your file is not there by printing out the file location and verifying it. For boolean tests, you don't need comparisons. Just do

    Java Code:
    if (!happyFile.exists()) {
       // create the file
    }
    If it doesn't exist, the method will return false. But placing the ! in front of the return changes it to true so the block will execute if the file is not there. You might also want to put a print statement in the creation block saying the file is being created until you are certain the logic is working properly.

    Regards,
    Jim
    Last edited by jim829; 08-12-2015 at 08:15 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: write file if it doesn't exist, but if it does exist do nothing?

    Quote Originally Posted by jim829 View Post
    Your syntax threw me for a minute. But it should have worked. You need to ensure your file is not there by printing out the file location and verifying it. For boolean tests, you don't need comparisons. Just do

    Java Code:
    if (!happyFile.exists()) {
       // create the file
    }
    If it doesn't exist, the method will return false. But placing the ! in front of the return changes it to true so the block will execute if the file is not there. You might also want to put a print statement in the creation block saying the file is being created until you are certain the logic is working properly.

    Regards,
    Jim
    I tried if(happyFile.exists() != true) which didn't work
    and if(!happyFile.exists() == true) which also didn't work. I'm not sure why I can't write exactly, as the file never appeared.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: write file if it doesn't exist, but if it does exist do nothing?

    I just noticed something. Check your braces. I don't see a brace before the else statement. And you need to use more print statements. Print out the absolute path name of where you think the file is. Then check there. And make certain it is not hidden. Sprinkling print statements thru out the code is the first basic debugging technique.

    Regards,
    Jim
    Last edited by jim829; 08-12-2015 at 09:30 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Join Date
    Aug 2015
    Location
    Northern California
    Posts
    31
    Rep Power
    0

    Default Re: write file if it doesn't exist, but if it does exist do nothing?

    Quote Originally Posted by innovo View Post
    I tried if(happyFile.exists() != true) which didn't work
    and if(!happyFile.exists() == true) which also didn't work. I'm not sure why I can't write exactly, as the file never appeared.
    innovo yours isn't like jim is showing you. The if statement tests, if true, you do not add == true. If you want to reverse the logic you use the invert logic operator ! (also known as the not operator)
    Java Code:
    public class WhatsTrue {
    
        public static void main(String[] args) {
            System.out.println(""+!true);
    
        }
    
    }
    Will print: false

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: write file if it doesn't exist, but if it does exist do nothing?

    Does that code compile?
    'happyFile' is declared inside the if statement....
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: write file if it doesn't exist, but if it does exist do nothing?

    Quote Originally Posted by Tolls View Post
    Does that code compile?
    'happyFile' is declared inside the if statement....
    I don't see why not, probably that new local variable will overshadow a class property.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: write file if it doesn't exist, but if it does exist do nothing?

    In which case it's pointless.
    Why not simply use the existing happyFile instance?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 3
    Last Post: 08-03-2013, 07:21 PM
  2. Replies: 4
    Last Post: 06-09-2012, 09:30 PM
  3. Package Doesn't exist - Issue with import
    By Sagars in forum New To Java
    Replies: 4
    Last Post: 08-08-2011, 05:36 PM
  4. Exist or not exist file problem
    By Ballister in forum New To Java
    Replies: 6
    Last Post: 12-26-2010, 07:29 PM
  5. getImage - not fail when image doesn't exist
    By leonid.ge in forum Java Applets
    Replies: 0
    Last Post: 07-24-2009, 03:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •