I'm trying to write a JSON string in txt file that contains other JSON objects. The structure of the txt file is:
{
"array": [
{
"id": "1",
"owner": "email_string",
"title": "title_string",
"content": "content_string"
},
{
"id": "2",
"owner": "email_string",
"title": "title_string",
"content": "content_string"
}
]
}
Now in my code, I have built the new String JSON formatted to append after the last object but I don't know how to write the new JSON string after the last JSON object and before the ']' character. The DropboxFile object has method as getInputStream(), getWriteStream(), getAppendStream(). I suppose i should use one of them but I don't know how. Can anyone help me?
Here is the code where i need to put the writing:
DbxFileSystem dbxFs;
dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());
DbxPath path = new DbxPath(NOTE_DB_PATH);
dbxFs.syncNowAndWait();
DbxFile file = dbxFs.open(path);
String JSONNote = JSONParser.buildJSONNoteString(title, content, currentUserEmail, id)+"\n";
// TODO Here I have to find the place to write my JSONNote string
dbxFs.syncNowAndWait();
file.close();
dbxFs.shutDown();
} catch (DbxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
SOLUTION:
DbxFileSystem dbxFs;
dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());
DbxPath path = new DbxPath(NOTE_DB_PATH);
dbxFs.syncNowAndWait();
DbxFile file = dbxFs.open(path);
String JSONNote = JSONParser.buildJSONNoteString(title, content, currentUserEmail, id)+"\n";
JSONObject newJsonObj = new JSONObject(JSONNote);
String dataString = readFile(file);
JSONObject dataJSON = new JSONObject(dataString);
dataJSON.accumulate("array", newJsonObj);
file.writeString(dataJSON.toString());
dbxFs.syncNowAndWait();
file.close();
dbxFs.shutDown();
readfile() method:
protected String readFile(DbxFile file) throws DbxException, IOException{
FileInputStream in = file.getReadStream();
int size = in.available();
byte c[] = new byte[size];
for (int i = 0; i < size; i++) {
c[i] = (byte) in.read();
}
String filedata = new String(c, "utf-8");
in.close();
return filedata;
}
First read your file using following code
public String readFile(String filepath) throws IOException {
File f = new File(filepath);
FileInputStream in = new FileInputStream(f);
int size = in.available();
byte c[] = new byte[size];
for (int i = 0; i < size; i++) {
c[i] = (byte) in.read();
}
String filedata = new String(c, "utf-8");
return filedata;}
Then form JSONArray as i have looked at u r JSON structure you can do it in following way
String data = readFile("your filepath");
JSONObject data = new JSONObject(data);
JSONArray newarray = (JSONArray)data.get("array");
Then you can use "put" method of JSONArray and append data in existing jsonarray . so it will be like
newarray.put(index,your_data);
then use following code to write the same data to file
FileOutputStream fos = new FileOutputStream("your file name", false);
PrintStream ps = new PrintStream(fos);
ps.append(data.toString());
and hopefully you are done