How to rename a file on server that is mounted locally?
I try to rename a whole directory programmatically. The directory is on a server that is mounted on the local file system. I'm trying it to do like this:
public static void main(String[] args) {
File dir = new File("/Volumes/video/Serien/Scrubs/Season 1");
System.out.println("Start renaming: " + dir);
String[] files = dir.list();
for (String file : files) {
System.out.println("Old name: " + file);
File renamedFile = new File(file);
System.out.println(renamedFile.toString());
boolean success = renamedFile.renameTo(new File("Test " + renamedFile.toString()));
System.out.println("New n开发者_开发知识库ame: "+ renamedFile.toString());
System.out.println(success);
break;
}
}
I now that it tries only to rename the first one, but nevertheless it returns false and doesn't rename.
So any hints why? I do not get any exceptions. I think it is because the server requires authentication?
Edit: Since renameTo() seems to be platform-dependent: I'm using Lion OSX
Try using a fullpath + the directory name when you are trying to rename for both old and renamed directory. I believe list() returns the directory name only without the fullpath. I had similar problem before and it worked when I did that. Hopefully that works for you as well.
精彩评论