Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: renaming/deleting file

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default renaming/deleting file

    Hi again, I now have a problem where I rename a file, and delete another one, and neither one of the actions are actually happening.

    It looks like this:
    static File rfile = new File(dir + "points.txt");
    File rfile4 = new File(dir + "temp.txt");
    rfile4.renameTo(rfile);
    		          System.out.println(rfile4);
    		          rfile.delete();

    I have nothing happening after this, yet when I go to check if anything happened, both files are still there, and neither have been renamed. To prove it, the data that I entered into rflie4 before renaming it is still there (and the file is called "temp"). The System.out.println statement returns "C:\Users\John\workspace\Various classes\temp.txt" when it should return "C:\Users\John\workspace\Various classes\points.txt."

  2. The Following User Says Thank You to The_Mexican For This Useful Post:

    javapenguin (January 15th, 2011)


  3. #2
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: renaming/deleting file

    how did you define dir variable? try also to remove the "static" keyword.
    Last edited by JavaHater; January 9th, 2011 at 08:05 PM.

  4. #3
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: renaming/deleting file

    renameTo(File dest) returns a boolean that indicates whether the rename operation was successful or not. I would recommend checking this return value to determine what behavior you should expect.

    From the javadoc:

    Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

  5. #4
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: renaming/deleting file

    Quote Originally Posted by DavidFongs View Post
    renameTo(File dest) returns a boolean that indicates whether the rename operation was successful or not. I would recommend checking this return value to determine what behavior you should expect.

    From the javadoc:
    Ok, so I checked the return value and saw that they both came up as false. But now is there a way I could make it work? Does renameTo() just not work if there's always a file by that name?

  6. #5
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: renaming/deleting file

    Quote Originally Posted by JavaHater View Post
    how did you define dir variable? try also to remove the "static" keyword.
    dir is basically where the program is running from. I can't remove the static because I was taking these lines somewhat out of context, there is a reason I need the static there.

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: renaming/deleting file

    Given DavidFong's post above noting the platform dependence of this function and the behavior you noted, there is always the ugly option of doing it the brute-force way (open and read the file, create/write a new file with the new name and write the contents, finally deleting the old file).

  8. #7
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: renaming/deleting file

    From your original post, its obvious you are using Windows to do this. On a windows system, what you are trying to do will fail if a file already exists with the name you want to rename the temp file to. The operation will also fail if the source file that you are trying to rename doesn't exist (but that isn't your issue here).

    Since you are renaming rfile4 to rfile (effectively overwriting rfile) why not just call rfile.delete() before the rename call

  9. #8
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: renaming/deleting file

    Quote Originally Posted by DavidFongs View Post
    From your original post, its obvious you are using Windows to do this. On a windows system, what you are trying to do will fail if a file already exists with the name you want to rename the temp file to. The operation will also fail if the source file that you are trying to rename doesn't exist (but that isn't your issue here).

    Since you are renaming rfile4 to rfile (effectively overwriting rfile) why not just call rfile.delete() before the rename call
    I tried what you suggested but it still came up as false and nothing happened It guess the only way this will work is doing what copeg suggested?

  10. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: renaming/deleting file

    1. What OS are you running under? With Windows Vista/7, certain areas require elevated privileges in order to modify (such as the Program Files folder).
    2. Do the original file and the new file both exist according to Java? Check using rfile4.exists() and rfile.exists().

  11. #10
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: renaming/deleting file

    Quote Originally Posted by helloworld922 View Post
    1. What OS are you running under? With Windows Vista/7, certain areas require elevated privileges in order to modify (such as the Program Files folder).
    2. Do the original file and the new file both exist according to Java? Check using rfile4.exists() and rfile.exists().
    1. Windows Vista. I seriously doubt I would need elevated privileges in order to delete these text files. I've tried deleting them manually and it works.

    2. Yes, I checked using the way you suggested.

  12. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: renaming/deleting file

    Hmm... I'm not entirely sure what's going on. Perhaps it's an issue with your installed software? What version of the JDK and JRE do you have? The latest JDK/JRE is Java SE 6 update 23.

  13. #12
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: renaming/deleting file

    Quote Originally Posted by helloworld922 View Post
    Hmm... I'm not entirely sure what's going on. Perhaps it's an issue with your installed software? What version of the JDK and JRE do you have? The latest JDK/JRE is Java SE 6 update 23.
    I have Java SE 6 Update 21. I guess I'll update it to 23 but I doubt that's the problem...

  14. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: renaming/deleting file

    it was a slim chance

  15. #14
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: renaming/deleting file

    Well I guess I'll just do it the manual way copeg suggested. Thanks anyway guys!

    EDIT: Ah yes! I partly figured it out! Ok, so what needed to happen is that I had to delete/rename the files after I closed the FileInputStream, which I wasn't doing before. However, there is still a small problem. I can only either delete one or rename the other. For example, if I delete rfile first then I can't rename rfile4. If I rename rfile4 first then I can't delete rfile. Any ideas on how I should do this?
    Last edited by The_Mexican; January 12th, 2011 at 12:58 PM.

  16. #15
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Re: renaming/deleting file

    Hmmmmm....file has a method called renameTo(File dest).

    If you're renaming it, it's going somewhere else in memory.

    File also has a method called delete().

    I don't see why you can't do one or the other?

    Why would you rename it and then delete it right away for anyway?

    If you called renameTo and then called delete in the same place you're doing this operation, it should be able to do both. Have to look at the code to know more.
    Also, any idea how to update JDK to 6.23 so that I can use AWTUtilities?

    And why were you printing out a File?

    Did you mean to print out

    file.getName() instead?



    You could post the entire code. There might be something going wrong outside of what you've shown. *

    Also, I've added stars to your ratings, as this is a good thread, and it'll also attract more attention I think as it'll stand out.
    Last edited by javapenguin; January 15th, 2011 at 12:24 AM.

  17. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: renaming/deleting file

    This might also be a long shot, but why is one of your File objects static and the other isn't?

Similar Threads

  1. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM
  2. Drag & drop MVC: deleting an object
    By Alice in forum Object Oriented Programming
    Replies: 0
    Last Post: December 9th, 2010, 11:09 PM
  3. [SOLVED] Can someone verify if this code for deleting a BST works?
    By scottb80 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 2nd, 2010, 10:19 AM
  4. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM