首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android File.exists() 大小寫敏感性取決於文件系統:FAT32 不敏感,ext4 敏感

     編輯:程式設計綜合問答

File.exists() 對大小寫敏感與否由底層文件系統決定,非 Java/API 行為。

文件系統差異

存儲位置文件系統大小寫敏感示例
SD 卡FAT32 / exFAT不敏感/sdcard/dd == /sdcard/DD
內部存儲ext4 / f2fs敏感/data/file != /data/FILE

代碼驗證

new File("/sdcard/file").exists();  // true
new File("/sdcard/FILE").exists();  // true  (FAT32 不敏感)
new File("/data/file").exists();    // true
new File("/data/FILE").exists();    // false (ext4 敏感)

最佳實踐

  • 代碼中統一使用小寫路徑,避免跨文件系統問題。
  • 若需精確匹配大小寫(如校驗用戶輸入),在敏感文件系統下列舉目錄對比:
File dir = new File("/sdcard");
String[] files = dir.list();
for (String f : files) {
    if (f.equalsIgnoreCase("dd")) { // 邏輯層面忽略大小寫
        // 找到
    }
}

知曉文件系統特性,路徑大小寫處理不再踩坑。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved