Solved

Java - code to ignore if file exists

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 95
Eclipse Java import and method not resolved 4 45
hashmap order 17 36
Running JavaFX on the Raspberry Pi 27 37
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 about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

808 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