12 Replies - 6458 Views - Last Post: 15 October 2013 - 04:25 PM Rate Topic: -----

#1 shango11   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-July 11

renameTo(Folder) always returns false.

Posted 15 October 2013 - 02:18 PM

I'm trying to use the renameTo(File) function to rename a folder but for some reason the function always returns false which means it failed to change the name of the folder. The folder hierarchy is:
top
-dir1 (inside top)
--txt files (inside dir1)
-dir2 (inside top)
--dir3 (inside dir2)
---txt files (inside dir3)

I'm using the following function to try and change the name.
String x is the folder name that I want to change and String y is the new name for the folder.
	public static void renameDirectory(String x, String y)
	{
		//System.out.println(x);
		//System.out.println(y);		
		String file = new File("").getAbsolutePath();
		String files = new File("").getAbsolutePath();
		file = file+"\\top\\"+x;
		files = files+"\\top\\"+y;
		//System.out.println(file);
		//System.out.println(files);
		File filer = new File(file);
		File filed = new File(files);
		boolean renamed = filer.renameTo(filed);
		System.out.println(renamed);
        }


Any ideas why it keeps failing to change the folder name?

Is This A Good Question/Topic? 0
  • +

Replies To: renameTo(Folder) always returns false.

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: renameTo(Folder) always returns false.

Posted 15 October 2013 - 02:44 PM

A question for you first - what is your intention with this?

Quote

String file = new File("").getAbsolutePath();

Was This Post Helpful? 1
  • +
  • -

#3 shango11   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-July 11

Re: renameTo(Folder) always returns false.

Posted 15 October 2013 - 02:55 PM

View Postg00se, on 15 October 2013 - 02:44 PM, said:

A question for you first - what is your intention with this?

Quote

String file = new File("").getAbsolutePath();


I use it so I don't have to type out the full path every time. I used it on a couple of functions on the same program and it seemed to help so I thought it would be a good idea to keep using it throughout. Is that the cause of the failed rewriting?
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: renameTo(Folder) always returns false.

Posted 15 October 2013 - 02:59 PM

Well it's not 'correct' but more about that later. The important thing is that you should be aware the result is unpredictable. And since you append directories to that unpredictable path, that's not (necessarily) going to work.

What are you intending to be the arguments to the method you wrote?
Was This Post Helpful? 1
  • +
  • -

#5 shango11   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-July 11

Re: renameTo(Folder) always returns false.

Posted 15 October 2013 - 03:07 PM

View Postg00se, on 15 October 2013 - 02:59 PM, said:

Well it's not 'correct' but more about that later. The important thing is that you should be aware the result is unpredictable. And since you append directories to that unpredictable path, that's not (necessarily) going to work.

What are you intending to be the arguments to the method you wrote?


I ask the user for the folder name they want to change (so dir1 or dir2 or dir3) and then I ask the user for a new name for the folder (dir11 or dir21 or dir42) and I just try to change the name of the folder. "x" is the original folder name and y is the new folder name.
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: renameTo(Folder) always returns false.

Posted 15 October 2013 - 03:15 PM

OK, so it's a relative path. How do you know from which directory they are going to run your app?
Was This Post Helpful? 1
  • +
  • -

#7 shango11   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-July 11

Re: renameTo(Folder) always returns false.

Posted 15 October 2013 - 03:27 PM

View Postg00se, on 15 October 2013 - 03:15 PM, said:

OK, so it's a relative path. How do you know from which directory they are going to run your app?

I do not know from which directory they are going to run the app. It's a school project so I'm guessing they will run the app from the same directory that contain the folder hierarchy mentioned on the first post, otherwise the app won't work.
Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: renameTo(Folder) always returns false.

Posted 15 October 2013 - 03:40 PM

You can't ever really predict it with certainty. Therefore you can't effectively hardcode relative paths. So either use absolute paths or perhaps use File instances got from a JFileChooser
Was This Post Helpful? 1
  • +
  • -

#9 shango11   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-July 11

Re: renameTo(Folder) always returns false.

Posted 15 October 2013 - 03:53 PM

View Postg00se, on 15 October 2013 - 03:40 PM, said:

You can't ever really predict it with certainty. Therefore you can't effectively hardcode relative paths. So either use absolute paths or perhaps use File instances got from a JFileChooser


So I would need to do something like this?
	public static void renameDirectory(String x, String y)
	{
		File filer = new File("C:\\***\\***\\***\\App\\Lab3\\top\\dir1");
		File filed = new File("C:\\***\\***\\***\\App\\Lab3\\top\\dir11");
		boolean renamed = filer.renameTo(filed);
		System.out.println(renamed);
         }


Was This Post Helpful? 0
  • +
  • -

#10 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: renameTo(Folder) always returns false.

Posted 15 October 2013 - 03:57 PM

Probably yes, but it's not clear where your parameters come in with that code

For a proper chance of that working, you should maybe do something like

if (filer.exists() && !filed(exists)) {
 boolean renamed = filer.renameTo(filed);
}

This post has been edited by g00se: 15 October 2013 - 04:00 PM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

#11 shango11   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 06-July 11

Re: renameTo(Folder) always returns false.

Posted 15 October 2013 - 04:02 PM

View Postg00se, on 15 October 2013 - 03:57 PM, said:

Probably yes, but it's not clear where your parameters come in with that code


If that's the only way to get it to work my parameters wouldn't matter so I basically need to find an alternative to the renameTo() function or scrap the whole renaming idea.

This post has been edited by shango11: 15 October 2013 - 04:03 PM

Was This Post Helpful? 0
  • +
  • -

#12 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: renameTo(Folder) always returns false.

Posted 15 October 2013 - 04:11 PM

Quote

If that's the only way to get it to work
Well as i mentioned earlier, if you got your user to use a JFileChooser, you'd have no problem at all
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: renameTo(Folder) always returns false.

Posted 15 October 2013 - 04:25 PM

View Postshango11, on 15 October 2013 - 04:18 PM, said:

String x is the folder name that I want to change and String y is the new name for the folder.


In that case, wouldn't it make more sense to call them "source" and "target" or "old" and "new" or something like that?
"x" and "y" make me look for a point on a plane, and there's no way the generated documentation will help anyone figure out the order of the arguments.


Quote

Any ideas why it keeps failing to change the folder name?


I have to second the idea of a file chooser here. It'd be a lot easier to deal with. However, if you really want to work with this structure, you might want to test the paths to see if they're sane. If the target directory doesn't exist, or the user can't write to it, then you'd like to have your method send up a whimper of despair instead of just dying silently. (Check the File class documentation for suitable methods)



That's if I'm thinking from the design perspective. If I think from the homework perspective, I think some different thoughts:

What is the actual assignment? If the conditions that you've cited are part of the assignment definition, then we can hopefully count on the program being run in a way that has a chance of working. In that case, it might be best to ensure that you have the requisite structures on your machine, and trust that your teacher's grading routine will be set up with the right things when you turn in the assignment. So two things I'd suggest in that regard are:
- write a small program (or even a method of your current program) to verify that the correct directory structures are in place, relative to where your program thinks is "here"
- write a small program or a method in your current program which creates the correct structures relative to "here" if they do not exist. (ie, use the previous method in this one)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1