Results 1 to 18 of 18
Like Tree4Likes
  • 1 Post By Tolls
  • 1 Post By gimbal2
  • 1 Post By DarrylBurke
  • 1 Post By gimbal2

Thread: Rename to doesn't rename the file even though it hits the code

  1. #1
    Fluffy93 is offline Member
    Join Date
    Aug 2014
    Posts
    32
    Rep Power
    0

    Default Rename to doesn't rename the file even though it hits the code

    I have two files - I can get the second one to populate with the first ones data and then delete the first one. I cannot get it to then rename itself to the name of the first file. It also only hits the 'rename' section of the code providing I place system.gc in otherwise it completely skips it. Anyone got any ideas on the matter or have experienced this before?

    the code I use is below:
    Java Code:
    finally{
            	  try {
            		     br.close();
                        
    				// mp br.close();
    			
    			//mp 	System.gc();
                File inputFile = new File("c:\\Maps.xml");
                System.gc();
    				 if(inputFile.delete()){
    	                   outputFile.renameTo(inputFile);
    	             System.out.println(outputFile);
    	                 }
    	
    			} catch (Exception e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
              }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Rename to doesn't rename the file even though it hits the code

    If System.gc() is "curing" the problem then that implies you have left a stream open somewhere which has gone out of scope. The gc call has (by pure fluke, as there's no guarantee when it will run) resulted in that stream being collected. I think the finaliser of a stream closes it.

    So you're still in a situation where there's an open stream.
    Fluffy93 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Rename to doesn't rename the file even though it hits the code

    ... but at least now there is more proof that it is the case. I assume an IDE is used, I would use it to do a text search in all source files to see which ones open file resources. A search for simply File might be the way to go.
    Fluffy93 likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Rename to doesn't rename the file even though it hits the code

    Quote Originally Posted by Tolls View Post
    The gc call has (by pure fluke, as there's no guarantee when it will run) resulted in that stream being collected.
    Although not mandated by the specs, all Sun/Oracle JVM implementations* actually do run the GC when System.gc() is invoked.

    It does run on its own thread, so there's no guarantee of completion before the next line of code is executed.

    db

    * Not any kind of insider information or in-depth study, just something I know from years on forums.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Rename to doesn't rename the file even though it hits the code

    I've had different results in the past in Java 6. I did a wonky plugin system with a custom classloader with the requirement to be able to "reload" plugins, which in those days meant being able to let the classloader be garbage collected. I actually had to call gc() three times in a row to get any kind of guarantee that garbage collection was done.

    HOWEVER, the problem is often in the way you test stuff. It might be because GC is done on a separate thread that it was actually the increased invocation time that did it. I can't test it anymore by putting a Thread.sleep() after the singular GC call :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Rename to doesn't rename the file even though it hits the code

    Quote Originally Posted by DarrylBurke View Post
    Although not mandated by the specs, all Sun/Oracle JVM implementations* actually do run the GC when System.gc() is invoked.

    It does run on its own thread, so there's no guarantee of completion before the next line of code is executed.

    db

    * Not any kind of insider information or in-depth study, just something I know from years on forums.
    Never knew that.
    But, in this case it's not just the gc, but also when the finalizer is run, which can be even later...depending on implementation.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Rename to doesn't rename the file even though it hits the code

    This is a little method I wrote to check the idea the first time I read it on the erstwhile SDN.
    Java Code:
    void testGC() {
      for (int i = 0; i < 100; i++) {
        JFrame f = new JFrame();
      }
      System.out.println(Frame.getFrame().length); // prints "100"
      System.gc();
      System.out.println(Frame.getFrame().length); // prints "0"
    }
    db
    gimbal2 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Fluffy93 is offline Member
    Join Date
    Aug 2014
    Posts
    32
    Rep Power
    0

    Default Re: Rename to doesn't rename the file even though it hits the code

    Thank you very much for all the replies. I have closed my buffered reader as I only use that and a filereader - closed that to for good measure. Perhaps it could be out of the scope but I don't think it is to be perfectly honest. The same thing still occurs - I did a file search Gimbal and I cleaned up the code and removed a variable that I did not need. I had two different variables using the same file. I still can't get it work. This is awfully confusing... it wouldn't even delete it that long ago so I'm making progress at least if only small progress..
    Last edited by Fluffy93; 10-21-2014 at 03:36 PM.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Rename to doesn't rename the file even though it hits the code

    Quote Originally Posted by DarrylBurke View Post
    This is a little method I wrote to check the idea the first time I read it on the erstwhile SDN.
    Java Code:
    void testGC() {
      for (int i = 0; i < 100; i++) {
        JFrame f = new JFrame();
      }
      System.out.println(Frame.getFrame().length); // prints "100"
      System.gc();
      System.out.println(Frame.getFrame().length); // prints "0"
    }
    db
    Well you can't make it more definitive than that :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Rename to doesn't rename the file even though it hits the code

    er, getFrame() should be getFrames()

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Rename to doesn't rename the file even though it hits the code

    Quote Originally Posted by Fluffy93 View Post
    Thank you very much for all the replies. I have closed my buffered reader as I only use that and a filereader - closed that to for good measure. Perhaps it could be out of the scope but I don't think it is to be perfectly honest. The same thing still occurs - I did a file search Gimbal and I cleaned up the code and removed a variable that I did not need. I had two different variables using the same file. I still can't get it work. This is awfully confusing... it wouldn't even delete it that long ago so I'm making progress at least if only small progress..
    I suspect it comes down to how your code is broken down.
    Something is not being closed, that's all we can say.
    We can't see your whole code here (and that's not an invite). Unless you can reduce it down to a real basic example which we could (given any old file) run, which means a complete flow, then there's little more we can do.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Fluffy93 is offline Member
    Join Date
    Aug 2014
    Posts
    32
    Rep Power
    0

    Default Re: Rename to doesn't rename the file even though it hits the code

    I wouldn't dream of putting my entire class up haha - I need to get my reader to not read in every single line of code too so I should probably try and focus on that. I genuinely can't see the problem and neither can the other java programmer so right now I think I may just have to leave it and try and come back to it. It works in a test file I did but it doesn't have any other code than renaming one file to another. I think I probably need to take a break and try and fix it again. Thanks though for all your help :)

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Rename to doesn't rename the file even though it hits the code

    Just to note, I wanted to post a more elaborate proof of concept yesterday but the forum didn't allow me as reported in this thread: http://www.java-forums.org/suggestio...not-found.html

    Let me try again.

    EDIT: nope, still cannot post it :/
    Last edited by gimbal2; 10-22-2014 at 10:43 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    Fluffy93 is offline Member
    Join Date
    Aug 2014
    Posts
    32
    Rep Power
    0

    Default Re: Rename to doesn't rename the file even though it hits the code

    I wonder why it won't let you post! I fixed it the only thing I changed was making all my readers declared globally so then I could have full access to them - anyway I closed everything readers and writers wise in my finally block rather than just one of them there and it works now so thank you for the help much appreciated!

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Rename to doesn't rename the file even though it hits the code

    Well I can at least point you to this link which shows you a way such that you don't have to manually clean up these resources:

    The try-with-resources Statement (The Java™ Tutorials > Essential Classes > Exceptions)
    Fluffy93 likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    Fluffy93 is offline Member
    Join Date
    Aug 2014
    Posts
    32
    Rep Power
    0

    Default Re: Rename to doesn't rename the file even though it hits the code

    Hi Gimbal thank you for the link - that is how I did it - not so great at explaining :P Thank you for posting the link to that however, i'll still give it a read so I'll keep it mind for future :)

  17. #17
    Fluffy93 is offline Member
    Join Date
    Aug 2014
    Posts
    32
    Rep Power
    0

    Default Re: Rename to doesn't rename the file even though it hits the code

    I tried to post a new thread and I got the same error you got Gimbal - is the site having difficulties?

  18. #18
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Rename to doesn't rename the file even though it hits the code

    If you have troubles, report them here: http://www.java-forums.org/suggestio...not-found.html
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. How to rename a file from one extension to other.
    By Hemanth Kumar in forum New To Java
    Replies: 1
    Last Post: 04-06-2012, 03:44 PM
  2. Copy,Delete,Rename File IO
    By jtyler in forum New To Java
    Replies: 0
    Last Post: 10-04-2010, 05:34 AM
  3. Unbale to delete and rename file
    By ddatta8 in forum New To Java
    Replies: 0
    Last Post: 01-31-2009, 02:16 PM
  4. How to rename a file
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 11:11 AM
  5. How to Rename a File
    By Ada in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •