Java 如何在地图中保存文件?
我正在开发字典应用程序 我有一个包含word的文件,它的翻译在行文件中。他们之间没有分歧。 我想将它们保存在地图中,以便搜索单词的翻译。 如何在地图中保存文件?Java 如何在地图中保存文件?,java,android,Java,Android,我正在开发字典应用程序 我有一个包含word的文件,它的翻译在行文件中。他们之间没有分歧。 我想将它们保存在地图中,以便搜索单词的翻译。 如何在地图中保存文件? 地图是如何工作的 您可以将地图另存为SharedReference,如下所示: private void saveMap(Map<String,Boolean> inputMap){ SharedPreferences pSharedPref = getApplicationContext().getSharedPref
地图是如何工作的 您可以将地图另存为SharedReference,如下所示:
private void saveMap(Map<String,Boolean> inputMap){
SharedPreferences pSharedPref = getApplicationContext().getSharedPreferences("MyVariables", Context.MODE_PRIVATE);
if (pSharedPref != null){
JSONObject jsonObject = new JSONObject(inputMap);
String jsonString = jsonObject.toString();
Editor editor = pSharedPref.edit();
editor.remove("My_map").commit();
editor.putString("My_map", jsonString);
editor.commit();
}
}
private Map<String,Boolean> loadMap(){
Map<String,Boolean> outputMap = new HashMap<String,Boolean>();
SharedPreferences pSharedPref = getApplicationContext().getSharedPreferences("MyVariables", Context.MODE_PRIVATE);
try{
if (pSharedPref != null){
String jsonString = pSharedPref.getString("My_map", (new JSONObject()).toString());
JSONObject jsonObject = new JSONObject(jsonString);
Iterator<String> keysItr = jsonObject.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Boolean value = (Boolean) jsonObject.get(key);
outputMap.put(key, value);
}
}
}catch(Exception e){
e.printStackTrace();
}
return outputMap;
}
private void保存映射(映射输入映射){
SharedReferences pSharedPref=getApplicationContext().GetSharedReferences(“MyVariables”,Context.MODE\u PRIVATE);
if(pSharedPref!=null){
JSONObject JSONObject=新的JSONObject(inputMap);
字符串jsonString=jsonObject.toString();
Editor-Editor=pSharedPref.edit();
editor.remove(“我的地图”).commit();
putString(“我的地图”,jsonString);
commit();
}
}
并按如下方式加载地图:
private void saveMap(Map<String,Boolean> inputMap){
SharedPreferences pSharedPref = getApplicationContext().getSharedPreferences("MyVariables", Context.MODE_PRIVATE);
if (pSharedPref != null){
JSONObject jsonObject = new JSONObject(inputMap);
String jsonString = jsonObject.toString();
Editor editor = pSharedPref.edit();
editor.remove("My_map").commit();
editor.putString("My_map", jsonString);
editor.commit();
}
}
private Map<String,Boolean> loadMap(){
Map<String,Boolean> outputMap = new HashMap<String,Boolean>();
SharedPreferences pSharedPref = getApplicationContext().getSharedPreferences("MyVariables", Context.MODE_PRIVATE);
try{
if (pSharedPref != null){
String jsonString = pSharedPref.getString("My_map", (new JSONObject()).toString());
JSONObject jsonObject = new JSONObject(jsonString);
Iterator<String> keysItr = jsonObject.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Boolean value = (Boolean) jsonObject.get(key);
outputMap.put(key, value);
}
}
}catch(Exception e){
e.printStackTrace();
}
return outputMap;
}
private Map loadMap(){
Map outputMap=newhashmap();
SharedReferences pSharedPref=getApplicationContext().GetSharedReferences(“MyVariables”,Context.MODE\u PRIVATE);
试一试{
如果(pSharedPref!=null){
String jsonString=pSharedPref.getString(“My_map”(新的JSONObject()).toString());
JSONObject JSONObject=新的JSONObject(jsonString);
迭代器keysItr=jsonObject.keys();
while(keysItr.hasNext()){
String key=keysItr.next();
布尔值=(布尔)jsonObject.get(键);
outputMap.put(键、值);
}
}
}捕获(例外e){
e、 printStackTrace();
}
返回outputMap;
}
请注意,
MyVariables
应该是唯一的静态字符串值。对不起,任何一行代码都会告诉编译器要从哪个文件读取数据?SharedReferences Api需要为值对设置密钥,以便能够存储和加载数据。只需查看官方文件和