Solved

Java - code to ignore if file exists

Posted on 2013-06-04
5
331 Views
Last Modified: 2013-06-04
HI:
I have a java app which takes a file from directory a, manipulates it, and copies the finished file to directory b. Works nicely, too - unless the file already exists. In that event the contents of the destination file are doubled up - an append, if you like.
Is there a java way of saying "if destination file already exists, do nothing; else do some code"
I enclose copies of my java code filesplitter.txt (yes I am a novice!), the source file (added 's' at end of number) and the destination file ('d'). If I run the code then the size of destination file will double - I need it only once.
Thanks!
182277s.txt
filesplitter.txt
182277d.txt
0
Comment
Question by:ClaytonGlass
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39218567
0
 

Author Comment

by:ClaytonGlass
ID: 39218626
Thanks CEHJ but I am not sure where to put the code. The app creates the file during a while loop; inserting the file.exists() in the loop simply truncates the loop. I am guessing that I need to run my file.exists() outside the loop - which suggest I need to duplicate the naming process earlier. I was hoping there may be an easier way; if anybody knows it...thanks anyway!
0
 
LVL 37

Accepted Solution

by:
zzynx earned 350 total points
ID: 39218706
Try replacing

 if(fw != null) fw.close();
 fw = new FileWriter(new File("J:\\saw\\"+curFile+ ".saw"), true);

Open in new window

by

 if(fw != null) fw.close();
 File outputFile = new File("J:\\saw\\"+curFile+ ".saw");
 if (!outputFile.exists()) {
   fw = new FileWriter(outputFile, true);
 }

Open in new window

0
 

Author Closing Comment

by:ClaytonGlass
ID: 39218745
Thanks you very much! and CEHJ, too!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 39218859
You're welcome.
Thanx 4 axxepting
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 25
throw exception 21 44
How do I remove an object from a 3 26
Convert from a json string array to a Java object 3 32
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question