英文:
Overwrite JSON object without replacing the file completely Android Studio
问题
基本上我试图覆盖以下JSON数据
{
"name" : "John Doe",
"gender" : "male",
"age" : "21"
}
我的目标是只替换年龄。所以我使用FileOutputStream如下
JSONObject object = new JSONObject();
try {
object.put("age", "40");
} catch (JSONException e) {
//做某事
}
String textToSave = object.toString();
try {
FileOutputStream fileOutputStream = openFileOutput("credentials.txt", MODE_PRIVATE);
fileOutputStream.write(textToSave.getBytes());
fileOutputStream.close();
intentActivity(MainMenu.class);
} catch (FileNotFoundException e) {
//做某事
} catch (IOException e) {
//做某事
}
但通过上述代码,我意识到它会完全删除现有的 credentials.txt
,这意味着我失去了 name
和 gender
的值。有没有办法只替换 age
?谢谢
英文:
Basically I am trying to overwrite the following JSON data
{
"name" : "John Doe",
"gender" : "male",
"age" : "21"
}
My goal is to replace only the age. So I am using FileOutputStream like below
JSONObject object = new JSONObject();
try {
object.put("age", "40");
} catch (JSONException e) {
//do smthng
}
String textToSave = object.toString();
try {
FileOutputStream fileOutputStream = openFileOutput("credentials.txt", MODE_PRIVATE);
fileOutputStream.write(textToSave.getBytes());
fileOutputStream.close();
intentActivity(MainMenu.class);
} catch (FileNotFoundException e) {
//do smthng
} catch (IOException e) {
//do smhtng
}
But with the above code, I realized it completely deletes the existing credentials.txt
which means I lost the name
and gender
value. Is there anyway just to replace the age
? Thank you
答案1
得分: 0
基本上回答你的问题是否定的。你的操作步骤是:
- 将文件读入内存,
- 在内存中修改内容,
- 使用修改后的内容覆盖旧文件。
以下是有关如何修改文本文件的问题的答案链接:在Java中修改现有文件内容。
英文:
Basically answer to your question is no. Your course of action is to
- read the file into memory,
- modify the content in memory
- override the old file with modified content.
Here is the question with good answers on how to modify a text file: Modifying existing file content in Java
答案2
得分: 0
我找到了答案。基本上,我需要读取现有的JSON文件,然后替换现有的JSON对象值。
private void writeFile(String age) {
try {
FileInputStream fileInputStream = openFileInput("credentials.json");
InputStreamReader isr = new InputStreamReader(fileInputStream);
BufferedReader br = new BufferedReader(isr);
StringBuilder jsonStringBuilder = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
jsonStringBuilder.append(line);
}
br.close();
String jsonString = jsonStringBuilder.toString();
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(jsonString).getAsJsonObject();
jsonObject.addProperty("age", age);
FileOutputStream fileOutputStream = openFileOutput("credentials.json", MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream);
BufferedWriter bw = new BufferedWriter(osw);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String updatedJsonString = gson.toJson(jsonObject);
bw.write(updatedJsonString);
bw.flush();
bw.close();
} catch (FileNotFoundException e) {
intentActivity(Login.class);
} catch (IOException e) {
intentActivity(Login.class);
}
}
英文:
I've found the answer to this. Basically I need to read the existing JSON file and then replace the existing JSON object value
private void writeFile(String age) {
try {
FileInputStream fileInputStream = openFileInput("credentials.json");
InputStreamReader isr = new InputStreamReader(fileInputStream);
BufferedReader br = new BufferedReader(isr);
StringBuilder jsonStringBuilder = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
jsonStringBuilder.append(line);
}
br.close();
String jsonString = jsonStringBuilder.toString();
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(jsonString).getAsJsonObject();
jsonObject.addProperty("age", age);
FileOutputStream fileOutputStream = openFileOutput("credentials.json", MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream);
BufferedWriter bw = new BufferedWriter(osw);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String updatedJsonString = gson.toJson(jsonObject);
bw.write(updatedJsonString);
bw.flush();
bw.close();
} catch (FileNotFoundException e) {
intentActivity(Login.class);
} catch (IOException e) {
intentActivity(Login.class);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论