android 文件存储对文件名大小写不敏感

1、开发中发现,当两个文件名只有大小写的区别,其他都一样的情况,android中会默认是同一个文件。

       比如,你在同一个文件夹下有一个文件 aa.txt   重新创建一个文件命名为AA.txt  android系统会认为这俩是同一个文件,从而出现覆盖的问题。

       经测试发现,windows、Mac osx 系统也是这样的。

2、项目中遇到的问题是。创建文档时防止文件重名导致的覆盖问题。使用 file.exist()方法来判断文件是否存在,经测试发现:

       存储路径时sd卡时,该方法不区分大小写,但是路径在data/目录下时,改方法区分大小写。  但是只是该方法区分大小写,实际存入data/目录下的文件名系统还是不区分大小写,所以当文件路径是data/时,就不能使用此方法判断文件是否存在。

       

如果你有两个文件,/sdcard/file (在 SD卡)和 /data/file (在内部文件系统), 你会得到以下结果:
new File("/sdcard/file").exists(); // true
new File("/sdcard/FILE").exists(); // true, /sdcard是一个不区分大小写的文件系统 
new File("/data/file").exists(); // true
new File("/data/FILE").exists(); // false, /数据是区分大小写的文件系统
//从数据库读出该文件夹下的所有文件,然后把名称转换成小写存入集合,把用户数据的文件名字也转为小写,然后进行判断重名。
mModel.loadFiles(FixedFolderHelper.DOCUMENTFOLDER, new CallBack<ArrayList<Files>>() {
            @Override
            public void onSuccess(ArrayList<Files> datas) {
                ArrayList<String> names = new ArrayList<>();
                for (Files file : datas) {
                    String fileName = file.getFileName();
                    names.add(fileName.toLowerCase());
                }
                String newName_1 = newName + Constant.MYDOCUSUFFIX;
                if (!names.contains(newName_1.toLowerCase())) {
                    String filePath = FixedFolderHelper.DOCUMENTFOLDER + "/" + newName_1;
                    saveFile(filePath, inputText);

                } else {
                    mView.showToast("文件名已存在");
                }
                
                //当存储路径为sd卡时使用此方法比较简洁,但如果存储路径是data/目录下时,需要使用上面的方法判断。
//String newName_1 = newName + Constant.MYDOCUSUFFIX;
//        String filePath = FixedFolderHelper.DOCUMENTFOLDER + "/" + newName_1;
//        File file = new File(filePath);
//        if (!file.exists()) {//存储路径是在sd卡时,可以使用此方法。
//        saveFile(filePath, inputText);
//        } else {
//        mView.showToast("文件名已存在");
//
//        }

            }

            @Override
            public void onFailed(String errorDetail) {

            }
        });
原文地址:https://www.cnblogs.com/epmouse/p/6511301.html