Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

renaming an existing file

Posted on 1997-10-31
2
Medium Priority
?
527 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 800 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

660 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