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 6 of 6

Thread: Problems with "renameTo" - method in the File class

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Problems with "renameTo" - method in the File class

    Hello!

    Consider the following code:

    public class TestUppgift1 {
     
        public static void main(String [] arg) {
     
            Scanner sc = new Scanner(new InputStreamReader(System.in));
            String katalog = "";
            File fil0 = new File("C:\\Hej\\a");
            File fil02 = new File("C:\\Hej\\b");
            File fil = new File(fil0, "hejsan.txt");
            File fil2 = new File(fil02, "dal.txt");
            if(fil.exists()) {
     
                System.out.println("Du har fattart!");
     
                if(fil2.exists()) {
     
                    if(fil.renameTo(fil2)==true) {
                        System.out.println(fil.getName());
                    }
     
     
                }
            }
        }
     
    }

    Why can't I rename fil?

    Hank


  2. #2
    Junior Member Tjones787's Avatar
    Join Date
    Jan 2014
    Location
    Lagos, Nigeira
    Posts
    16
    My Mood
    Asleep
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Problems with "renameTo" - method in the File class

    Hi, kindly tell us, what error are you getting when you run the program.
    Tjones787

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Problems with "renameTo" - method in the File class

    I don't get an error at all. The renameTo - method return false.

    Hank

  4. #4
    Junior Member Tjones787's Avatar
    Join Date
    Jan 2014
    Location
    Lagos, Nigeira
    Posts
    16
    My Mood
    Asleep
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Problems with "renameTo" - method in the File class

    When you print the name of the file. What was your output

    --- Update ---

    renameTo method return a true or false argument to tell you if the file was successfully renamed
    Tjones787

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Problems with "renameTo" - method in the File class

    Quote Originally Posted by iHank View Post
                if(fil2.exists()) {
     
                    if(fil.renameTo(fil2)==true) {
    From javadoc documentation of renameTo: "it might not succeed if a file with the destination abstract pathname already exists"

    If fil2.exists() is true, you are exactly in the condition stated above!
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  6. The Following 2 Users Say Thank You to andbin For This Useful Post:

    aprabhat (January 24th, 2014), iHank (January 22nd, 2014)

  7. #6
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Problems with "renameTo" - method in the File class

    Quote Originally Posted by andbin View Post
    From javadoc documentation of renameTo: "it might not succeed if a file with the destination abstract pathname already exists"

    If fil2.exists() is true, you are exactly in the condition stated above!
    Thank you! Now I understand the renameTo-method.

    Hank

Similar Threads

  1. Replies: 3
    Last Post: August 14th, 2013, 04:23 PM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Find the important class in ".mse" file using Java program
    By ashwarth21 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 13th, 2012, 07:43 PM
  4. Replies: 1
    Last Post: December 15th, 2011, 07:29 AM
  5. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM