![](/img/trans.png)
[英]In java, how to write an object to a file and later on read it from the file and convert it back to the original object in the HDFS?
[英]Read From JSON File and after adding a property to that object write back to it
我的 java 項目中有一個空的 json 文件,只有 json 對象括號:- 那是
一個.json
{}
我想從中讀取並在添加一個鍵值后寫回它,即:-
{"a":"a"}
有沒有我可以使用的 Json 庫。
我不想在 java 中使用簡單的 FileReaders 和 FileWriters 來讀寫。
這個任務有幾個庫。
一個可能是 Jettison https://mvnrepository.com/artifact/org.codehaus.jettison/jettison
Path filePath = <your-file-path>;
String json = new String(Files.readAllBytes(filePath));
JSONObject jsonObject = new JSONObject(json);
jsonObject.put("a", "a");
Files.write(filePath, jsonObject.toString().getBytes());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.