文件的方法 renameTo() 返回 true,但如果我更改文件名的小写/大写,实际上不会重命名文件



例如:

String oldName = "SubFolder1";
String newName = "subfolder1";
File oldFolder = new File(getExternalFilesDir("some_folder").toString() + File.separator + oldName);
File newFolder = new File(getExternalFilesDir("some_folder").toString() + File.separator + newName);
boolean isSuccesfullyRenamed = oldFolder.renameTo(newFolder);
Log.v("rename success", isSuccessfullyRenamed);

抱歉,如果代码无法编译,但通常这将返回 TRUE 表示成功重命名,但文件夹并未"实际"从"子文件夹 1"重命名为"子文件夹 1"。

为什么它会返回成功?我如何实际更改Android上已经存在的文件夹/文件的字母大小写?

我能想到的唯一解决方案是创建一个临时父文件夹,将文件/子文件夹移动到该临时文件夹,然后将其移回旧的父文件夹。

linux system equalIgnoreCase file or folder name so both 
name are same so that return true and try this below code:
private void renameFile()
{
String oldName = "SubFolder1";
String newName = "subfolder2";
String root= 
Environment.getExternalStorageDirectory().toString()+
"/"+"some_folder";
File oldFolder = new File(root,File.separator+oldName);
File newFolder = new File(root,File.separator+newName);
boolean isSuccesfullyRenamed = 
oldFolder.renameTo(newFolder);
Log.d("renamesuccess", isSuccesfullyRenamed+"");
}

相关内容

  • 没有找到相关文章

最新更新