Reputation: 381
java.nio.file.Path newfile = FileSystems.getDefault().getPath("/home/krishnaprasad/report.json");
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-------");
FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(perms);
if(!Files.exists(newfile)) {
Files.createFile(newfile, attrs);
}
Files.write(newfile, jsonarr.toString().getBytes(), StandardOpenOption.APPEND);
I'm append the JSONArray to the file. It is appending in the same line. Can I append it in the next line?
Upvotes: 1
Views: 471
Reputation: 97381
Just add a newline before the JSON:
String content = System.lineSeparator() + jsonarr;
Files.write(newfile, content.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.APPEND);
Upvotes: 2