Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Files.createFile()将数据写入新行? java.nio.file.Path newfile=FileSystems.getDefault().getPath(“/home/krishnaprasad/report.json”); 设置perms=PosixFilePermissions.fromString(“rw------”); FileAttribute attrs=PosixFilePermissions.asFileAttribute(perms); 如果(!Files.exists(newfile)){ createFile(newfile,attrs); } write(newfile,jsonarr.toString().getBytes(),StandardOpenOption.APPEND);_Java - Fatal编程技术网

如何使用Files.createFile()将数据写入新行? java.nio.file.Path newfile=FileSystems.getDefault().getPath(“/home/krishnaprasad/report.json”); 设置perms=PosixFilePermissions.fromString(“rw------”); FileAttribute attrs=PosixFilePermissions.asFileAttribute(perms); 如果(!Files.exists(newfile)){ createFile(newfile,attrs); } write(newfile,jsonarr.toString().getBytes(),StandardOpenOption.APPEND);

如何使用Files.createFile()将数据写入新行? java.nio.file.Path newfile=FileSystems.getDefault().getPath(“/home/krishnaprasad/report.json”); 设置perms=PosixFilePermissions.fromString(“rw------”); FileAttribute attrs=PosixFilePermissions.asFileAttribute(perms); 如果(!Files.exists(newfile)){ createFile(newfile,attrs); } write(newfile,jsonarr.toString().getBytes(),StandardOpenOption.APPEND);,java,Java,我将把JSONArray附加到文件中。它在同一行中追加。我可以在下一行添加它吗?只需在JSON之前添加一个新行: java.nio.file.Path newfile = FileSystems.getDefault().getPath("/home/krishnaprasad/report.json"); Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-------"); Fi

我将把JSONArray附加到文件中。它在同一行中追加。我可以在下一行添加它吗?

只需在JSON之前添加一个新行:

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);

你没有
Files.createNewFile()
不做任何输出,事实上这里的
exists()
createNewFile()
调用都是毫无意义和浪费的。还有其他方法可以处理吗?
String content = System.lineSeparator() + jsonarr;

Files.write(newfile, content.getBytes(StandardCharsets.UTF_8),
        StandardOpenOption.APPEND);