I have to change the title casing of a folder. Basically, on app startup I want to look for the old casing, and rename it if it is still there. So far in my tests, android ignores the casing, and reports the folder casing as whatever you send in. So basically
new File(PATH_WITH_OLD_CASING) == new File(PATH_WITH_NEW_CASING)
and it also seems to report the absolute path that you send in, without regards to what it is actually named in the file system. so
new File(PATH_WITH_OLD_CASING).getAbsolutePath() == PATH_WITH_OLD_CASING;
and
new File(PATH_WITH_NEW_CASING).getAbsolutePath() == PATH_WITH_NEW_CASING;
even though they refer to the same file.
So I am struggling to come up with a way to determine if the old开发者_StackOverflow casing is still there. I can probably do a listFiles on the parent directory to get the casing, but its the root of the SD Card, don't really want to do that everytime the app starts (doesn't seem like a very clean solution).
Any ideas?
You say the directory is on the SD card, and that is most likely where your problem lies.
By default, the SD card is formatted as FAT, which preserves case but is case insensitive.
This meand that /sdcard/Hello is the same as /sdcard/HeLlO.
Now, the reason why File
is not behaving as you expect, I believe, is because it is operating on the pathname you pass it, rather than reading what is present on the filesystem. Even if you open the file and it succeeds, I still wouldn't expect that it would update the path you give it, I'd expect it to just maintain an internal flag that the file was successfully opened.
You should use getCanonicalPath() rather than getAbsolutePath():
Most callers should use getAbsolutePath() instead. A canonical path is significantly more expensive to compute, and not generally useful. The primary use for canonical paths is determining whether two paths point to the same file by comparing the canonicalized paths.
UPDATE: a not exactly ideal solution I came up with
if (parentDirectory.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.contentEquals(DEPRECATED_FILE_NAME);
}
}).length > 0){
//old file exists
}
You could always convert the file name to a string value and then use the toUpper or toLower functions to check for the old file. It's a bit of an extra step in your solution, but might be the quickest way to accomplish what you're trying to do.
hmm... first try to list directory. sample code for listing directory is here
after that compare them to your file name. hope it will work
精彩评论