現在、gson
オブジェクトのシリアル化を実行するために を使用しています。単一のプラットフォーム (Windows) では問題なく動作します。
ただし、異なるプラットフォーム (Windows、Linux、Mac、Android) で json ファイルを共有する場合、特定の種類のエンコーディング (UTF-8) を使用する必要がありますか? (jsonファイルに外国語の文字が入ります)?または、BufferedWriter/BufferedReader で使用されるデフォルトのエンコーディングは、すべてのプラットフォームで同じになりますか?
public static boolean write(A a, File file) {
final Gson gson = new Gson();
String string = gson.toJson(a);
try {
//If the constructor throws an exception, the finally block will NOT execute
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
try {
writer.write(string);
} finally {
//no need to check for null
//any exceptions thrown here will be caught by
//the outer catch block
writer.close();
}
} catch (IOException ex){
return false;
}
return true;
}
public static A load(File file) {
// Copy n paste from newInstance.
final Gson gson = new Gson();
try {
//If the constructor throws an exception, the finally block will NOT execute
BufferedReader reader = new BufferedReader(new FileReader(file));
try {
return gson.fromJson(reader, A.class);
} finally {
//no need to check for null
//any exceptions thrown here will be caught by
//the outer catch block
reader.close();
}
} catch (IOException ex){
}
return null;
}