Solved

Java - code to ignore if file exists

Posted on 2013-06-04
5
328 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now