Solved

renaming an existing file

Posted on 1997-10-31
2
522 Views
Last Modified: 2010-05-18
How do you rename an existing file on a local drive? In my application, I save a new file to a temporary name, and then rename it to the real name after I finish writing data to disk. In the method doSave(), I pass a File object that I create based on information the user specifies in a FileDialog. Note that theFile doesn't exist yet, it simply describes the file's pathname:



private boolean doSave(File theFile)

{

// create a unique file name for the temp file

String tempName = "_" + String.valueOf(System.currentTimeMillis());

File tempFile = new File(theFile.getParent(), tempName);



// create a RandomAccessFile object and pass to application's method

RandomAccessFile aRaf = new RandomAccessFile(tempFile, "rw");

myApp.writeData(aRaf);

// data is safely written to hard disk, so now rename temp file to real file name I want

boolean savedOK = (tempFile.renameTo(theFile));

return savedOK;

}



The method always returns false. The data is written to disk correctly, but the call to renameTo never works, and the file still has the temporary name.
0
Comment
Question by:dacton
[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 Comments
 
LVL 4

Accepted Solution

by:
russgold earned 200 total points
ID: 1229705
You cannot rename a file while it is open.  

Add the line:

aRAF. close();

before:

tempFile. rename( theFile );
0
 

Author Comment

by:dacton
ID: 1229706
Yikes! I should have figured that one out. I added the close() call it it all works now. Thanks for the quick response.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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