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

5

13

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!

Salomo answered 28/6, 2011 at 6:54 Comment(0)
G
35

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
Gaiser answered 28/6, 2011 at 7:11 Comment(1)
The /sdcard/file I used is on internal memory actually, Not SD card, the rear SD card on Android is /sdcard/SD_CARD/. So, both /sdcard/dd and /sdcard/DD are on a Linux file system.Salomo
T
2

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

Takeover answered 11/12, 2018 at 5:39 Comment(0)
R
0

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).

Renitarenitent answered 28/6, 2011 at 7:11 Comment(1)
Actually, FAT does matter - Android can only work with what the filesystem supports, so if the filesystem (FAT in this case) is not case sensitive, then Android cannot provide case sensitivity for that filesystem. To do otherwise would be to create a new, incompatible variant of FAT. This is entirely known, and expected behaviour.Feeler
A
0

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.

Amphibian answered 28/6, 2011 at 7:12 Comment(1)
Windows filesystems are generally case insensitive, but Linux filesystems are case sensitive. If you use a Windows style filesystem on Linux (including Android), then it will not be case sensitive (but typically does preserve case)Feeler
K
-2

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.

Kalif answered 26/2, 2016 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.