如何使用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);
我将把JSONArray附加到文件中。它在同一行中追加。我可以在下一行添加它吗?只需在JSON之前添加一个新行:如何使用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
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);