Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Renaming files

  • 12-07-2011 6:00pm
    #1
    Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭


    Hey guys,

    having a bit of trouble renaming a file. The question in my book asks to create a new file called test.txt and let the user rename it. here is the code i have but when i run its calling up the old name. Any bit of help would be great


    import java.io.*;
    import java.util.*;

    class book{
    public static void main(String args[]){
    Scanner in = new Scanner(System.in);

    File newFile = new File("test.txt");

    System.out.println(newFile.getName());

    System.out.println("Enter new file name");
    String newName=in.nextLine();

    File newFile2=new File(newName);

    newFile.renameTo(newFile2);
    System.out.println(newFile);

    }
    }


    Thanks!!


Comments

  • Registered Users, Registered Users 2 Posts: 15,065 ✭✭✭✭Malice


    I don't really know Java but according to here it returns true if the naming succeeded so check that to see if it actually worked. Also check the value of newFile2 to make sure you don't have a return character or spaces in there.


  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    Malice wrote: »
    I don't really know Java but according to here it returns true if the naming succeeded so check that to see if it actually worked. Also check the value of newFile2 to make sure you don't have a return character or spaces in there.

    Hey thanks for the reply. I checked out the link and I figured out where i went wrong. I just created an instance of the new file but i didnt actually create it. I have that sorted now thanks!!


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    It looks like you're creating a second new file with the second filename, then trying to change the first file to have the new filename. This will fail because the second file you create will already have that filename. I think you shouldn't be creating the second file, just read in the new name and rename the first (and only) file to that.


  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    stevenmu wrote: »
    It looks like you're creating a second new file with the second filename, then trying to change the first file to have the new filename. This will fail because the second file you create will already have that filename. I think you shouldn't be creating the second file, just read in the new name and rename the first (and only) file to that.


    Is it not just an instance of a new file im creating?? I wont be able to check it out until later on today. But il try doing it with out creating the second file.


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Not totally sure how it works with Java, it might be just an instance of the File class and not a physical file behind it, but you shouldn't need the second instance anyway.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    stevenmu wrote: »
    Not totally sure how it works with Java, it might be just an instance of the File class and not a physical file behind it, but you shouldn't need the second instance anyway.


    That makes sense alright doesnt it. Il try doin it without the second instance later on report back! Thanks!!


  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    ok i managed to get it workin with out creating the second instance of File, but in the console the file name does not change. From what ive found on google this is a windows problem


  • Closed Accounts Posts: 25 iDigian


    I know as much about java as I do about women which is diddly. However, logically the following might help,

    IF File instantiates a new File Object but doesn't associate it with the file itself then you will most likely need to save it before trying to rename it, I don't know, maybe theres some confusion because you already have a test.txt in your execution directory etc. Just a shot in the dark.

    Also you are outputting the Object and not the newFile.getName Method
    ie System.out.println(newFile);
    as Opposed to System.out.println(newFile.getName());
    Again not knowing Java I don't know whether it returns nothing, the type name (if this was the case I think you would have copped it already) or the filename.


  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    Here is my code now. Its creating the file on my desktop and renaming it. But when i try to print the file name in the console its printing the original name. Its very strange.


    import java.util.Scanner;
    import java.io.*;
    import java.util.*;
    class testing{
    public static void main(String args[]){
    Scanner in = new Scanner(System.in);


    File newFile= new File("C:\\Users\\Public\\Desktop\\bren.txt"); //creates a new instance of file
    try{
    newFile.createNewFile(); //actually creates the file
    newFile.renameTo(new File("C:\\Users\\Public\\Desktop\\john.txt")); //renames file
    }

    catch(Exception e){

    }
    System.out.println(newFile.getName());



    }

    }


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    jonny666 wrote: »
    Here is my code now. Its creating the file on my desktop and renaming it. But when i try to print the file name in the console its printing the original name. Its very strange.

    Its not strange at all. Files like Strings are immutable. Have a look at the javadoc for java.io.File and read the first paragraph.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    lynchie wrote: »
    Its not strange at all. Files like Strings are immutable. Have a look at the javadoc for java.io.File and read the first paragraph.

    You will have to forgive me if I'm being a bit stupid here. I'm only starting out! Does that mean I cannot actually change the name of the file that comes up on the console one its declared?


  • Closed Accounts Posts: 25 iDigian


    lynchie wrote: »
    Its not strange at all. Files like Strings are immutable. Have a look at the javadoc for java.io.File and read the first paragraph.


    I'm a little confused by that comment. I can't see how it being immutable is anything to do with it, sorry.


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    jonny666 wrote: »
    You will have to forgive me if I'm being a bit stupid here. I'm only starting out! Does that mean I cannot actually change the name of the file that comes up on the console one its declared?
    iDigian wrote: »
    I'm a little confused by that comment. I can't see how it being immutable is anything to do with it, sorry.

    It means that once a file has been instantiated, the abstract pathname associated with the file will NEVER change. There is no way to change the path once it is created except for instantiating a new instance of the class with a new path, thus meaning it is immutable.

    As File instances are only references to a real file system object, when you rename a file instance, only the underlying file system file is changed, the file instance is not.

    To answer your question OP, to print out the new file name, use the reference to the second File instance you created.
        public static void main(String[] args) throws IOException
        {
            File file1= new File("C:\\temp\\bren.txt"); //creates a new instance of file
            File file2= new File("C:\\temp\\john.txt");
    
            System.out.println("File1 Exists:"+file1.isFile()); // Should be false
            System.out.println("File2 Exists:"+file2.isFile()); // Should be false
            
            if(file1.createNewFile()) //actually creates the file
            {
                System.out.println("File1 Exists:"+file1.isFile()); // Should be true as the file instance is associated with the underlying file system
                System.out.println("File2 Exists:"+file2.isFile()); // Should be false
                
                System.out.println(file1.getName()); // <--- Will always print out the same name
                System.out.println(file2.getName()); // <--- Will always print out the same name
                
                if(file1.renameTo(file2))
                {
                    //underlying file system file has been renamed but the file instances still refer to their original pathnames
    
                    System.out.println("File1 Exists:"+file1.isFile()); // Should be false as the underlying file system file was renamed
                    System.out.println("File2 Exists:"+file2.isFile()); // Should be true as this instance is now associated with the underlying file system file
                    
                    System.out.println(file1.getName()); // <--- Will always print out the same name
                    System.out.println(file2.getName()); // <--- Will always print out the same name
                }
            }
        }
    
    


  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    lynchie wrote: »
    It means that once a file has been instantiated, the abstract pathname associated with the file will NEVER change. There is no way to change the path once it is created except for instantiating a new instance of the class with a new path, thus meaning it is immutable.

    As File instances are only references to a real file system object, when you rename a file instance, only the underlying file system file is changed, the file instance is not.

    To answer your question OP, to print out the new file name, use the reference to the second File instance you created.
        public static void main(String[] args) throws IOException
        {
            File file1= new File("C:\\temp\\bren.txt"); //creates a new instance of file
            File file2= new File("C:\\temp\\john.txt");
     
            System.out.println("File1 Exists:"+file1.isFile()); // Should be false
            System.out.println("File2 Exists:"+file2.isFile()); // Should be false
     
            if(file1.createNewFile()) //actually creates the file
            {
                System.out.println("File1 Exists:"+file1.isFile()); // Should be true as the file instance is associated with the underlying file system
                System.out.println("File2 Exists:"+file2.isFile()); // Should be false
     
                System.out.println(file1.getName()); // <--- Will always print out the same name
                System.out.println(file2.getName()); // <--- Will always print out the same name
     
                if(file1.renameTo(file2))
                {
                    //underlying file system file has been renamed but the file instances still refer to their original pathnames
     
                    System.out.println("File1 Exists:"+file1.isFile()); // Should be false as the underlying file system file was renamed
                    System.out.println("File2 Exists:"+file2.isFile()); // Should be true as this instance is now associated with the underlying file system file
     
                    System.out.println(file1.getName()); // <--- Will always print out the same name
                    System.out.println(file2.getName()); // <--- Will always print out the same name
                }
            }
        }
     
    


    Thats exactly what i needed to hear! I was gettin confused with the actual file and the File object. I can move on now.

    Thanks very much everyone!!!


  • Closed Accounts Posts: 25 iDigian


    lynchie wrote: »
    It means that once a file has been instantiated, the abstract pathname associated with the file will NEVER change. There is no way to change the path once it is created except for instantiating a new instance of the class with a new path, thus meaning it is immutable.

    As File instances are only references to a real file system object, when you rename a file instance, only the underlying file system file is changed, the file instance is not.

    To answer your question OP, to print out the new file name, use the reference to the second File instance you created.
        public static void main(String[] args) throws IOException
        {
            File file1= new File("C:\\temp\\bren.txt"); //creates a new instance of file
            File file2= new File("C:\\temp\\john.txt");
     
            System.out.println("File1 Exists:"+file1.isFile()); // Should be false
            System.out.println("File2 Exists:"+file2.isFile()); // Should be false
     
            if(file1.createNewFile()) //actually creates the file
            {
                System.out.println("File1 Exists:"+file1.isFile()); // Should be true as the file instance is associated with the underlying file system
                System.out.println("File2 Exists:"+file2.isFile()); // Should be false
     
                System.out.println(file1.getName()); // <--- Will always print out the same name
                System.out.println(file2.getName()); // <--- Will always print out the same name
     
                if(file1.renameTo(file2))
                {
                    //underlying file system file has been renamed but the file instances still refer to their original pathnames
     
                    System.out.println("File1 Exists:"+file1.isFile()); // Should be false as the underlying file system file was renamed
                    System.out.println("File2 Exists:"+file2.isFile()); // Should be true as this instance is now associated with the underlying file system file
     
                    System.out.println(file1.getName()); // <--- Will always print out the same name
                    System.out.println(file2.getName()); // <--- Will always print out the same name
                }
            }
        }
     
    

    Forgive me but that's nothing to do with whether it's immutable or not. That's to do with the semantics of the renameTo method of the File object. Immutability in terms of strings which you compared to originally refers to memory management and how a string is treated internally.


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    iDigian wrote: »
    Forgive me but that's nothing to do with whether it's immutable or not. That's to do with the semantics of the renameTo method of the File object.
    It has everything to do with it being immutable. The internal representation of path within File remains constant once created. When an objects internal state cannot change once created it is deemed immutable. The fact the OP was not aware of this led to the assumption that the path name would change when the file was renamed.

    iDigian wrote: »
    Immutability in terms of strings which you compared to originally refers to memory management and how a string is treated internally.

    Agreed, java makes strings immutable specifically for memory management and efficiency so that it can maintain a string pool within the jvm at runtime


  • Closed Accounts Posts: 25 iDigian


    lynchie wrote: »
    It has everything to do with it being immutable. The internal representation of path within File remains constant once created. When an objects internal state cannot change once created it is deemed immutable. The fact the OP was not aware of this led to the assumption that the path name would change when the file was renamed.




    Agreed, java makes strings immutable specifically for memory management and efficiency so that it can maintain a string pool within the jvm at runtime

    Hmmmm. Again IF that's the way it operates that's the behaviour of the object ie how the class was written. It doesn't make it immutable comparative to string immutability.


Advertisement