?
Solved

JAVA - Overwrite file if exists

Posted on 2009-04-15
5
Medium Priority
?
3,943 Views
Last Modified: 2012-05-06
I would like to overwrite the file if it exists. I am writing to a csv file and I dont want to append to it but rather overwrite the file if it matches the name exactly.

How would I modify it?

Date now = new Date();
formatter = new SimpleDateFormat("MMddyyyy");
s = formatter.format(now);

outputFile = "C:\\Files\\NJ" + s +".CSV";

// Error catching.
try
{
      File file = new File( outputFile );
      fileExists = file.exists();
      
      out = new FileWriter( outputFile, true );
      session.log( "Writing data to a file." );
      if (!fileExists)
      {
                      etc,etc,etc
                      }
0
Comment
Question by:ralphcarter2008
  • 3
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24152122
>>I would like to overwrite the file if it exists.

That's the default behaviour of Java, so you have no problem
0
 
LVL 19

Expert Comment

by:LordOfPorts
ID: 24152142
Try passing "false" as the second parameter for the second parameter of the FileWriter constructor:

out = new FileWriter( outputFile, false );
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24152230
You don't need the second parameter at all. Just use out = new FileWriter( outputFile, false );
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24152241
Sorry, i mean



out = new FileWriter( outputFile);

then there's no possibility of error
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 24153602
LordOfPorts is right, change that true to false
you can also get rid of that exists logic

Date now = new Date();
formatter = new SimpleDateFormat("MMddyyyy");
s = formatter.format(now);

outputFile = "C:\\Files\\NJ" + s +".CSV";

// Error catching.
try
{
      boolean append = false;
      File file = new File( outputFile );
     
      out = new FileWriter( outputFile, append );
      session.log( "Writing data to a file." );
   
       etc,etc,etc


0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

750 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