如何在Android中重命名现有的共享首选项文件

如何在Android中重命名现有的共享首选项文件,android,sharedpreferences,Android,Sharedpreferences,我是android新手。我创建了SharedReferences,用于在播放列表中存储播放列表名称和歌曲名称。现在我必须重新命名播放列表 另一个问题是:当我删除播放列表时,如何删除SharedReferences文件(即playlyname.xml),因为您选择了播放列表的最佳存储空间。数据库更适合您的需要。 尽管如此,您仍然可以使用基本java io删除sp文件。最后,我可以重命名SharedReference文件 作为参考,在我的上下文中,代码是: String fileName=etlis

我是android新手。我创建了SharedReferences,用于在播放列表中存储播放列表名称和歌曲名称。现在我必须重新命名播放列表


另一个问题是:当我删除播放列表时,如何删除SharedReferences文件(即
playlyname.xml
),因为您选择了播放列表的最佳存储空间。数据库更适合您的需要。
尽管如此,您仍然可以使用基本java io删除sp文件。

最后,我可以重命名SharedReference文件

作为参考,在我的上下文中,代码是:

String fileName=etlistName.getText().toString();
File f=new File("/data/data/eywa.musicplayer/shared_prefs/"+PlayListName+".xml");
f.renameTo(new File("/data/data/eywa.musicplayer/shared_prefs/"+fileName+".xml"));

SharedPreferences mySharedPreferences=getSharedPreferences("list_of_playlist",Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.remove(PlayListName);
editor.putString(fileName, fileName);
editor.commit();
PlayListName=fileName;
用于删除playlyname.xml:

for (int i=0; i<selectedItems.size();i++)
{//remove the songs names from the playlist
    SharedPreferences sp=getSharedPreferences(selectedItems.get(i),Activity.MODE_PRIVATE);
    SharedPreferences.Editor ed=sp.edit();
    ed.clear();
    ed.commit();
    //remove the play list name from the list_of_playlist
    SharedPreferences.Editor editor = mainPref.edit();
    editor.remove(selectedItems.get(i));
    //delete .xml file
    File f=new File("/data/data/eywa.musicplayer/shared_prefs/"+selectedItems.get(i)+".xml");
    if(f.delete())
        System.out.println("file deleted")
    editor.commit();
}
selectedItems.clear();
for(int i=0;i从“/data/data/…”访问文件是不可靠的,因为我认为并非所有手机的路径都是相同的(即三星设备是不同的AFAIK)

我更喜欢下面的方法,它基本上是“复制”旧的共享prefs,然后将其清除。此方法不会删除旧的共享prefs文件本身,而是更可靠

SharedPreferences settingsOld = context.getSharedPreferences(nameOld, Context.MODE_PRIVATE);
SharedPreferences settingsNew = context.getSharedPreferences(nameNew, Context.MODE_PRIVATE);
SharedPreferences.Editor editorNew = settingsNew.edit();
Map<String, ?> all = settingsOld.getAll();
for (Entry<String, ?> x : all.entrySet()) {

    if      (x.getValue().getClass().equals(Boolean.class)) editorNew.putBoolean(x.getKey(), (Boolean)x.getValue());
    else if (x.getValue().getClass().equals(Float.class))   editorNew.putFloat(x.getKey(),   (Float)x.getValue());
    else if (x.getValue().getClass().equals(Integer.class)) editorNew.putInt(x.getKey(),     (Integer)x.getValue());
    else if (x.getValue().getClass().equals(Long.class))    editorNew.putLong(x.getKey(),    (Long)x.getValue());
    else if (x.getValue().getClass().equals(String.class))  editorNew.putString(x.getKey(),  (String)x.getValue());

}
editorNew.commit();
SharedPreferences.Editor editorOld = settingsOld.edit();
editorOld.clear();
editorOld.commit(); 
SharedReferences settingsell=context.getSharedReferences(nameOld,context.MODE\u PRIVATE);
SharedReferences settingsNew=context.getSharedReferences(namew,context.MODE\u PRIVATE);
SharedReferences.Editor editorNew=settingsNew.edit();
Map all=settingsell.getAll();
对于(条目x:all.entrySet()){
如果(x.getValue().getClass().equals(Boolean.class))editorNew.putBoolean(x.getKey(),(Boolean)x.getValue());
如果(x.getValue().getClass().equals(Float.class))editorNew.putFloat(x.getKey(),(Float)x.getValue());
如果(x.getValue().getClass().equals(Integer.class))editorNew.putInt(x.getKey(),(Integer)x.getValue()),则为else;
如果(x.getValue().getClass().equals(Long.class))editorNew.putLong(x.getKey(),(Long)x.getValue()),则为else;
如果(x.getValue().getClass().equals(String.class))editorNew.putString(x.getKey(),(String)x.getValue());
}
editorNew.commit();
SharedReferences.Editor editorOld=SettingSeld.edit();
editorOld.clear();
editorOld.commit();

谢谢Vladimir。好的,我将使用数据库。删除sp文件时,我使用了“deleteFile(文件名)”'它不会删除文件。哎呀!代码被打扰了!请尝试理解。我是该网站的新手,android也感谢Vladimir Ivanov的建议。如果有帮助,你可能想投票给他的答案。我还提交了一份编辑,以帮助清理格式。