12 Replies Latest reply: Jul 20, 2007 9:45 AM by Puce RSS

    rename a file PROPERLY on disk

    807605
      Im trying to rename a file.. I know you're just going to tell me to use 'renameTo' but that doesnt actually change the files name on disk, it just changes to name of a 'File' object.

      How do I actually change the name of a physical file on disk that can be seen in a file browser or windows explorer? PERL can do this in one line :P
        • 1. Re: rename a file PROPERLY on disk
          masijade
          Im trying to rename a file.. I know you're just going
          to tell me to use 'renameTo' but that doesnt actually
          change the files name on disk
          , it just changes to
          name of a 'File' object.
          says who?

          Quote from API:
          Renames the file denoted by this abstract pathname.
          • 2. Re: rename a file PROPERLY on disk
            800282
            ... PERL can do this in one line :P
            Java too.
            • 3. Re: rename a file PROPERLY on disk
              807605
              nice, i like the helpful replies..
              my point is that renameTo didnt rename my files.. maybe i'll test it in a small program.

              Thanks for nothing :)
              • 4. Re: rename a file PROPERLY on disk
                800282
                nice, i like the helpful replies..
                my point is that renameTo didnt rename my files..
                maybe i'll test it in a small program.
                Your point and the thing you posted are two different things. In your original post you said that renameTo(...) didn't rename files, which is incorrect. The fact that it didn't rename YOUR file is something entirely different! You most probably did something wrong like not closing a stream, or your OS is locking the file.

                Thanks for nothing :)
                ?
                • 5. Re: rename a file PROPERLY on disk
                  800322
                  Are you still keeping a lock on that file somehow, e.g. by not closing a stream? It can't rename a file if you don't let it.
                  • 6. Re: rename a file PROPERLY on disk
                    807605
                    well im using Ubuntu Linux and im sure I have permission to alter the files.. Id appreciate it If somebody could show me how to rename files by either linking me to a site or posting some code.. I found this example:
                        // File (or directory) with old name
                        File file = new File("oldname");
                        
                        // File (or directory) with new name
                        File file2 = new File("newname");
                        
                        // Rename file (or directory)
                        boolean success = file.renameTo(file2);
                        if (!success) {
                            // File was not successfully renamed
                        }
                    can somebody explain it to me? does this actually alter files on disk, or just objects you have created?

                    if I did the following, would it actually rename the file i have on the disk, or just the variable 'file'?
                        // File (or directory) with old name
                        File file = new File("C\:\\music\\oldsongname\.mp3");
                        
                        // File (or directory) with new name
                        File file2 = new File("C\:\\music\\newsongname\.mp3");
                        
                        // Rename file (or directory)
                        boolean success = file.renameTo(file2);
                        if (!success) {
                            // File was not successfully renamed
                        }
                    sorry if my backslashes are in the wrong places, but you can see what i mean
                    • 7. Re: rename a file PROPERLY on disk
                      800282
                      As already stated:
                      - the file is really renamed on disk;
                      - if the returned value is false, then there is probably still a process that is locking the file (unclosed stream?).
                      • 8. Re: rename a file PROPERLY on disk
                        807605
                        import java.io.File;
                        
                        
                        public class MainClass {
                        
                             /**
                              * @param args
                              */
                             public static void main(String[] args) {
                                 // File (or directory) with old name
                                 File file = new File("/home/andy/testfile.txt");
                                 
                                 // File (or directory) with new name
                                 File file2 = new File("/home/andy/tesfilewithnewname.txt");
                                 
                                 // Rename file (or directory)
                                 boolean success = file.renameTo(file2);
                                 if (!success) {
                                      System.out.println("Error");
                                 }else{
                                      System.out.println("Done");
                                 }
                             }
                        }
                        That works! must be something to do with my code.. my program is too complicated to post here but i know what the problem is now
                        • 9. Re: rename a file PROPERLY on disk
                          800282
                          That works! must be something to do with my code.. my
                          program is too complicated to post here but i know
                          what the problem is now
                          9 out of 10 times it's an unclosed stream. You are probably reading/writing to the file in your code, right?
                          • 10. Re: rename a file PROPERLY on disk
                            Puce
                               
                            
                            // File (or directory) with old name
                            File file = new
                            File("C\:\\music\\oldsongname\.mp3");

                            // File (or directory) with new name
                            File file2 = new
                            File("C\:\\music\\newsongname\.mp3");
                            Are you sure these file names are correct (eg. file.exists() == true?)?
                            I think, these are neither valid Unix nor Windows file names.

                            On Windows it would probably be something like:
                            "C:\\music\\oldsongname.mp3"
                            On Unix: there's nothing like "C:"

                            -Puce

                            Message was edited by:
                            Puce
                            • 11. Re: rename a file PROPERLY on disk
                              807605
                              puce that was just an example.. its not important..

                              its not an open stream i dont think, im not actually writing to anything, im just renaming files.. im pretty sure its to do with me not being careful about my file names.. ie i think im passing it:
                              oldsong.mp3
                              rather than:
                              c:\music\oldsong.mp3
                              to keep with m original example.

                              I dont have the time right now but i just need to be sure of whats going on
                              • 12. Re: rename a file PROPERLY on disk
                                Puce
                                Debug it (using a debugger from an IDE such as NetBeans or Eclipse).
                                Also add a watch to see if your orignial file really exists.

                                -Puce