Solved

Java - code to ignore if file exists

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

729 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