Android - 将地图<String, Object[]> 保存到文件
Android - Save Map<String, Object[]> to file
如何将这种地图保存到文件中? (它也应该适用于 android 设备)
我试过了:
Properties properties = new Properties();
for (Map.Entry<String, Object[]> entry : map.entrySet()) {
properties.put(entry.getKey(), entry.getValue());
}
try {
properties.store(new FileOutputStream(context.getFilesDir() + MainActivity.FileName), null);
} catch (IOException e) {
e.printStackTrace();
}
我得到:
class java.util.ArrayList cannot be cast to class java.lang.String (java.util.ArrayList and java.lang.String are in module java.base of loader 'bootstrap')
我该怎么办?
我正在写一个基于 String
值序列化的答案,当我意识到您的错误时,也许某些值可以是 ArrayList
...老实说,我不完全理解背后的原因错误(当然是强制转换,但我不明白 java.util.ArrayList
部分)...
无论如何,当您尝试存储您的属性并且它试图将您的 Object[]
转换为 String
以进行保存时,问题就出现了。
在我最初的回答中,我建议您在生成文件时手动 join
您的值。 String
class:
的join
方法很简单
Properties properties = new Properties();
for (String key : map.keySet()) {
Object[] values = map.get(key);
// Perform null checking
String value = String.join(",", values);
properties.put(key, value);
}
try {
properties.store(new FileOutputStream(context.getFilesDir() + MainActivity.FileName), null);
} catch (IOException e) {
e.printStackTrace();
}
要读取您的值,您可以使用 split
:
Properties properties = new Properties();
Map<String, String> map = new HashMap<>();
InputStream in = null;
try {
in = new FileInputStream(context.getFilesDir() + MainActivity.FileName);
properties.load(in);
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(k);
// Perform null checking
String[] values = value.split(",");
map.put(key, value);
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
但我认为您有更好的方法:请使用 Java 内置序列化机制来保存和恢复您的信息。
为了节省您的 map
使用 ObjectOutputStream
:
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(context.getFilesDir() + MainActivity.FileName))){
oos.writeObject(map);
}
您可以按如下方式阅读您的map
:
Map<String, Object> map;
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(context.getFilesDir() + MainActivity.FileName))){
map = (Map)ois.readObject();
}
如果存储在 map
中的所有对象都是 Serializable
,则第二种方法要灵活得多,并且 不限于 到 String
值与第一个相同。
如何将这种地图保存到文件中? (它也应该适用于 android 设备)
我试过了:
Properties properties = new Properties();
for (Map.Entry<String, Object[]> entry : map.entrySet()) {
properties.put(entry.getKey(), entry.getValue());
}
try {
properties.store(new FileOutputStream(context.getFilesDir() + MainActivity.FileName), null);
} catch (IOException e) {
e.printStackTrace();
}
我得到:
class java.util.ArrayList cannot be cast to class java.lang.String (java.util.ArrayList and java.lang.String are in module java.base of loader 'bootstrap')
我该怎么办?
我正在写一个基于 String
值序列化的答案,当我意识到您的错误时,也许某些值可以是 ArrayList
...老实说,我不完全理解背后的原因错误(当然是强制转换,但我不明白 java.util.ArrayList
部分)...
无论如何,当您尝试存储您的属性并且它试图将您的 Object[]
转换为 String
以进行保存时,问题就出现了。
在我最初的回答中,我建议您在生成文件时手动 join
您的值。 String
class:
join
方法很简单
Properties properties = new Properties();
for (String key : map.keySet()) {
Object[] values = map.get(key);
// Perform null checking
String value = String.join(",", values);
properties.put(key, value);
}
try {
properties.store(new FileOutputStream(context.getFilesDir() + MainActivity.FileName), null);
} catch (IOException e) {
e.printStackTrace();
}
要读取您的值,您可以使用 split
:
Properties properties = new Properties();
Map<String, String> map = new HashMap<>();
InputStream in = null;
try {
in = new FileInputStream(context.getFilesDir() + MainActivity.FileName);
properties.load(in);
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(k);
// Perform null checking
String[] values = value.split(",");
map.put(key, value);
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
但我认为您有更好的方法:请使用 Java 内置序列化机制来保存和恢复您的信息。
为了节省您的 map
使用 ObjectOutputStream
:
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(context.getFilesDir() + MainActivity.FileName))){
oos.writeObject(map);
}
您可以按如下方式阅读您的map
:
Map<String, Object> map;
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(context.getFilesDir() + MainActivity.FileName))){
map = (Map)ois.readObject();
}
如果存储在 map
中的所有对象都是 Serializable
,则第二种方法要灵活得多,并且 不限于 到 String
值与第一个相同。