我收到了来自网络服务请求的响应,我可以在控制台上打印该响应。我想将此响应写入 json 文件。我尝试使用 buffereWriter 但它没有写入。
String cwd = System.getProperty("user.dir");
buffereWriter writer = new buffereWriter (new File writer(cwd + "filepath"));
Writer.write(response);
// This response is being currently displayed in the console correctly.
没有错误或异常,但内容没有写入文件。
最佳答案
您可以使用FileWriter
来实现这一点。还有一个带有资源的 try block
,它将保证您的文件将被关闭。
try(FileWriter fw = new FileWriter(cwd + "filepath")){
fw.write(response); //I guess the response is a String.
}catch(Exception ignored){}
关于java - 如何将 Web 服务响应写入 json 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54746460/