Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java - code to ignore if file exists

Posted on 2013-06-04
5
Medium Priority
?
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

636 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