Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

renaming file

Thread view: 
Scott Steiner - 23 Jun 2005 11:49 GMT
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!
TechBookReport - 23 Jun 2005 15:44 GMT
> hi,
>
[quoted text clipped - 13 lines]
>
> 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
Lucy - 23 Jun 2005 18:34 GMT
> > hi,
> >
[quoted text clipped - 13 lines]
> >
> > thx for any input!

This works for me:

File f = new File(name);
f.deleteOnExit();
Andrew Thompson - 23 Jun 2005 18:44 GMT
> This works for me:
>
>  File f = new File(name);
>  f.deleteOnExit();

It seems some other folks have found quirks to that
simple solution though..
<http://onesearch.sun.com/search/developers/index.jsp?qt=deleteOnExit&col=javabugs>

[ And.. why did you reply to Pan's post, when you
trimmed everything they wrote? ]

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Lucy - 23 Jun 2005 19:32 GMT
> > This works for me:
> >
[quoted text clipped - 3 lines]
> It seems some other folks have found quirks to that
> simple solution though..

<http://onesearch.sun.com/search/developers/index.jsp?qt=deleteOnExit&col=ja
vabugs>

> [ And.. why did you reply to Pan's post, when you
> trimmed everything they wrote? ]

I don't know, what's the answer?

> --
> Andrew Thompson
> http://www.PhySci.org/codes/  Web & IT Help
> http://www.PhySci.org/  Open-source software suite
> http://www.1point1C.org/  Science & Technology
> http://www.LensEscapes.com/  Images that escape the mundane
Raymond DeCampo - 24 Jun 2005 02:27 GMT
> hi,
>
[quoted text clipped - 11 lines]
> 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

Signature

XML is the programmer's duct tape.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.