renaming file

S

Scott Steiner

hi,

this problem has kept me busy for several days now. i'm implementing a
board game that logs the moves as they are done in a file. at the end of
the match the file is renamed by giving it a timestamp in it's name.
however, the renaming is not succeeding. actually i rename 2 files at
the end of the match, the first file is renamed but the second isn't.

what reasons can fundamentally cause this behaviour? the file is
definitely closed before attempting to rename it and no ther file exists
with the same name so there is no naming conflict after a rename. i'm
creating a file instance with new File(...) and then using the
renameTo() method. is there a way to get an error return code of the
renameTo() method which gives an explanation what exactly went wrong? at
the moment i only receive either true or false.

thx for any input!
 
T

TechBookReport

Scott said:
hi,

this problem has kept me busy for several days now. i'm implementing a
board game that logs the moves as they are done in a file. at the end of
the match the file is renamed by giving it a timestamp in it's name.
however, the renaming is not succeeding. actually i rename 2 files at
the end of the match, the first file is renamed but the second isn't.

what reasons can fundamentally cause this behaviour? the file is
definitely closed before attempting to rename it and no ther file exists
with the same name so there is no naming conflict after a rename. i'm
creating a file instance with new File(...) and then using the
renameTo() method. is there a way to get an error return code of the
renameTo() method which gives an explanation what exactly went wrong? at
the moment i only receive either true or false.

thx for any input!

It returns a boolean which you can test. I've hit a similar problem in
the past when deleting files, sometimes it just doesn't work (this is on
Windows, so the problem may not exist on Linux or other os).

For deleting a file I use this code:

private void deleteFile(File f){
for (int i=0;i<100;i++){
if (!f.delete()) {
System.gc();
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
// Ignore Exception
}
} else {
break;
}
}
}

Try it with rename and see if that solves the problem.

Pan
====================================================================
TechBookReport Java http://www.techbookreport.com/JavaIndex.html
 
R

Raymond DeCampo

Scott said:
hi,

this problem has kept me busy for several days now. i'm implementing a
board game that logs the moves as they are done in a file. at the end of
the match the file is renamed by giving it a timestamp in it's name.
however, the renaming is not succeeding. actually i rename 2 files at
the end of the match, the first file is renamed but the second isn't.

what reasons can fundamentally cause this behaviour? the file is
definitely closed before attempting to rename it and no ther file exists
with the same name so there is no naming conflict after a rename. i'm
creating a file instance with new File(...) and then using the
renameTo() method. is there a way to get an error return code of the
renameTo() method which gives an explanation what exactly went wrong? at
the moment i only receive either true or false.

The problem is the fundamental non-Java way File.renameTo() is
implemented, i.e., swallowing exceptions and returning a passcode.

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5036327>

One way is to open streams and copy the file manually, in process. Not
as efficient as a system call however.

HTH,
Ray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,745
Messages
2,569,485
Members
44,909
Latest member
DestinyKetoScam

Latest Threads

Top