此代码会将文本添加到新行,但有没有办法写入文件的最后一行?
try (FileWriter fileWriter = new FileWriter(file)) {
fileWriter.append("{\"receivedtime\":\""+dateReceived+"\", " + "\"text\":\""+text+"\"}");
fileWriter.flush();
}
正如您所看到的,我正在使用java编写JSON文件。但如果有另一行,则在行尾有这个逗号问题。所以我想如果我有一个新行我将添加逗号然后\ n然后行。对于最后一行,我不需要逗号,只需结束]}
{"employees":[
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}