dcsimg
<tt>renameTo()</tt> method in <tt>File</tt> class
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Chris_Peery
Posted On:   Tuesday, April 3, 2001 11:56 AM

I know the basics of the file class and have used many of the methods. The one I'm having trouble with the most is the renameTo() method. It seems to return true and change the file's name half the time, but the other half return false. I'm pretty sure it's a lock on the file that is not letting the name be changed. Below is some of my code. Basically this method I have created takes the files modified date and appends that to the files name and attempts to rename the existing file with the new formatted name. private static void saveFileWithDate(String strOldFile) { File oldFile = new File(strOldFile); try { if (oldFile.exists()) { String filename =    More>>

I know the basics of the file class and have used many of the methods.
The one I'm having trouble with the most is the renameTo() method.
It seems to return true and change the file's name half the time, but the other half return false.
I'm pretty sure it's a lock on the file that is not letting the name be changed. Below is some of
my code. Basically this method I have created
takes the files modified date and appends that to the files name and attempts to
rename the existing file with the new formatted name.

			
private static void saveFileWithDate(String strOldFile) {
File oldFile = new File(strOldFile);
try {
if (oldFile.exists()) {
String filename = oldFile.getName();
Date dateObj = new Date(oldFile.lastModified());

try {
SimpleDateFormat shortDateFormat
= new SimpleDateFormat ("yyyyMMdd");
strDate = shortDateFormat.format(dateObj);
} catch ( Exception ex ) {
catLog.error("Problem formatting date, " +strDate+ "; " + ex.getMessage());
}

int extIndex = filename.indexOf('.');
String extention = "";
if (extIndex>0) {
extention = filename.substring(extIndex,filename.length());
filename = filename.substring(0,extIndex);
}

//Here's where I create a new file instance with the modified date appended to the filename.
File saveFile = new File(filename+"_"+strDate+extention);
if (!oldFile.renameTo(saveFile)) {
catLog.error("Rename File did not succeed.");
}
}
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}

Anyway, if anyone can tell me why it sometimes won't rename the file and return false I would greatly appreciate it.    <<Less

Re: <tt>renameTo()</tt> method in <tt>File</tt> class

Posted By:   Simon_Clarke  
Posted On:   Thursday, September 6, 2001 11:35 AM

How did you fix the problem?

I am experiencing the same problem. I have followed the suggestions in the replies posted to your original problem but with no luck. I thought I'd give you a try to see if you solved the problem on your own.

Re: <tt>renameTo()</tt> method in <tt>File</tt> class

Posted By:   Tim_Rohaly  
Posted On:   Friday, April 20, 2001 08:22 PM

Probably because some other process has the
file open at that time. It is OS-dependent
behavior what happens when you try to rename
or delete a file that is open. It is giving
you the correct return value of false when
the operation fails - this gives you a programmatic
way to deal with this condition.
About | Sitemap | Contact