如何将sharedpreferences文件从内部存储传输到外部存储? [英] How to transfer sharedpreferences file from internal storage to external storage?

查看:586
本文介绍了如何将sharedpreferences文件从内部存储传输到外部存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 sharedpreferences 复制到 xml格式的外部存储中,以便稍后可以共享首选项。



尝试阅读 sharedpreferences 并保存为字符串创建 JSON 类型的字符串,但是我需要一个 xml 。想过遍历应用程序的内部存储和复制文件,并把它放在外部存储,但可能太复杂了。



只是真的想知道是否有一个简单的并且明智的方式来传递````````````````````````````````````` pre> SharedPreferences preferences = this.getSharedPreferences(com.example.application,Context.MODE_PRIVATE);
地图< String,?> keys = preferences.getAll();
属性properties = new Properties(); (Map.Entry< String,?> entry:keys.entrySet()){
String key = entry.getKey();
String value = entry.getValue()。toString();
properties.setProperty(key,value);
}
尝试{
File file = new File(externalPreferences.xml);
FileOutputStream fileOut = new FileOutputStream(file);
properties.storeToXML(fileOut,External Preferences);
fileOut.close();
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}

并回顾一下使用这个

  try {
File file = new File(externalPreferences.xml);
FileInputStream fileInput = new FileInputStream(file);
属性properties = new Properties();
properties.loadFromXML(fileInput);
fileInput.close();

枚举enuKeys = properties.keys();
SharedPreferences.Editor editor = preferences.edit();
while(enuKeys.hasMoreElements()){
String key =(String)enuKeys.nextElement();
String value = properties.getProperty(key);
editor.putString(key,value);
editor.commit();
}
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();

注意这个代码,

How to copy sharedpreferences to external storage keeping xml format, so later it could be possible to share preferences.

Tried to read sharedpreferences and save as a string into file, created JSON type of a string, but I need an xml. Thought of traversing through app's internal storage and copy file and put it in external storage, but that could be too complex.

Just really would want to know if there is an easy and sensible way to transfer `sharedpreferences.

解决方案

Use this code,

SharedPreferences preferences=this.getSharedPreferences("com.example.application", Context.MODE_PRIVATE);
Map<String,?> keys = preferences.getAll();
Properties properties = new Properties();
for(Map.Entry<String,?> entry : keys.entrySet()){
    String key = entry.getKey();
    String value = entry.getValue().toString();
    properties.setProperty(key, value);      
}
try {
    File file = new File("externalPreferences.xml");
    FileOutputStream fileOut = new FileOutputStream(file);
    properties.storeToXML(fileOut, "External Preferences");
    fileOut.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

and to retrive use this,

try {
    File file = new File("externalPreferences.xml");
    FileInputStream fileInput = new FileInputStream(file);
    Properties properties = new Properties();
    properties.loadFromXML(fileInput);
    fileInput.close();

    Enumeration enuKeys = properties.keys();
    SharedPreferences.Editor editor = preferences.edit();
    while (enuKeys.hasMoreElements()) {
        String key = (String) enuKeys.nextElement();
        String value = properties.getProperty(key);
        editor.putString(key, value);
        editor.commit();
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

NOTE You can handle only String type preferences with this code,

这篇关于如何将sharedpreferences文件从内部存储传输到外部存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆