Praveen Mandadi
Praveen Mandadi

Reputation: 381

How to write data to a new line using Files.createFile()?

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

Answers (1)

Robby Cornelissen
Robby Cornelissen

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

Related Questions