Isn't Android File.exists() case sensitive?

纵然是瞬间 提交于 2020-01-03 07:22:45

问题


I've created a new folder "sdcard/dd" by:

File album = new File(albumPath);

if (album.exists()) {
   Log.d(TAG, albumPath + " already exists.");
} else {
   boolean bFile = album.mkdir();
}

And Again, I create the second folder "sdcard/DD" by the same code, but, this time the album.exists() returns true, which indicates the "dd" is equals "DD".

Anyone know why the File.exists() can NOT check the case of the folder name? Thanks!


回答1:


While Linux, and therefore also Android, normally is case-sensitive when it comes to filenames, FAT file systems, which are often used on SD cards, memory sticks etc., are case-insensitive. Therefore, Android will not differentiate between cases when it is handling files on these file systems.

So if you have two files, /sdcard/file (on the SD card) and /data/file (on the internal file system), you will get the following results:

new File("/sdcard/file").exists(); // true
new File("/sdcard/FILE").exists(); // true, /sdcard is a case-insensitive file system
new File("/data/file").exists(); // true
new File("/data/FILE").exists(); // false, /data is a case-sensitive file system



回答2:


File exists IS case sensitive. I somehow expect you either aren't removing the first folder you created (sdcard/dd) or there is some odd sdcard file case-insensitivity (it IS FAT, which isn't case sensitive, but that really shouldn't matter).




回答3:


Try this in windows for example. the filename is not case sensitive. as is the case with linux (android being based on linux). navigating through directories also is recognized as case insensitive.

so dd and DD are both recongnized as the same path.




回答4:


As per Android documentation 'Android supports devices with traditional storage, which is defined to be a case-insensitive filesystem with immutable POSIX permission classes and modes.' https://source.android.com/devices/storage/traditional.html




回答5:


Files can be created case sensitively and are shown case sensitively even via ftp, but the exists() method does not distinguish. This is what it is like here in /storage/emulated/0/somepath on Android 5.1. I think this is inconsistent behaviour.



来源:https://stackoverflow.com/questions/6502712/isnt-android-file-exists-case-sensitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!