使用 Java 将 JSON 数据推送到文件

Push JSON data to a file using Java

我正在使用 java 调用 REST API Get 方法,并且 JSON 数据需要存储到文件中。 这是我的 JSON 数据:

{
 "took" : 25,
 "timed_out" : false,
 "_shards" : {
 "total" : 5,
 "successful" : 5,
 "failed" : 0
},
 "hits" : {
  "total" : 820,
  "max_score" : 1.0,
  "hits" : [ {
   "_index" : "test",
   "_type" : "status",
   "_id" : "abc-2345-dadasasdasd214124",
   "_score" : 1.0,
   "_source":
    {"A":1,"B":12,"C":122,"D":89,"E":120,"F":100,"G":2}
  }, {
   "_index" : "test",
   "_type" : "status",
   "_id" : "abc-2345-dadasasdasd214124",
   "_score" : 1.0,
   "_source":
    {"A":54,"B":171,"C":102,"D":0,"E":120,"F":11,"G":20}
 } , {
  "_index" : "test",
  "_type" : "status",
  "_id" : "abc-2345-dadasasdasd214124",
  "_score" : 1.0,
  "_source":
  {"A":94,"B":8,"C":155,"D":32,"E":90,"F":11,"G":0}
} ]
}
}

这是我的代码:

public class testcode {

public static void main(String[] args) throws ClientProtocolException, IOException {
      HttpClient client = new DefaultHttpClient();

      try {
          HttpGet httpGetRequest = new HttpGet("http://localhost:8080/test_search?pretty=1");
          HttpResponse httpResponse = client.execute(httpGetRequest);

          HttpEntity entity = httpResponse.getEntity();

          byte[] buffer = new byte[1024];
          if (entity != null) {
            InputStream inputStream = entity.getContent();
            try {
              int bytesRead = 0;
              BufferedInputStream bis = new BufferedInputStream(inputStream);
              FileWriter file = new FileWriter("/path/test.json");

              while ((bytesRead = bis.read(buffer)) != -1) {
                String chunk = new String(buffer, 0, bytesRead);
                System.out.println(chunk);
                file.write(chunk);


              }
            } catch (Exception e) {
              e.printStackTrace();
            } finally {
              try { inputStream.close(); } catch (Exception ignore) {}
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          client.getConnectionManager().shutdown();
        }

}

在 运行 这段代码之后,我在我的控制台中看到了 JSON 数据,但整个数据没有保存到 test.json 文件中。前两个数组被完全保存,但只有第三个数组的一部分被保存到 test.json.

test.json 文件中的数据:

{
 "took" : 25,
  "timed_out" : false,
  "_shards" : {
  "total" : 5,
  "successful" : 5,
  "failed" : 0
},
 "hits" : {
  "total" : 820,
  "max_score" : 1.0,
  "hits" : [ {
   "_index" : "test",
   "_type" : "status",
   "_id" : "abc-2345-dadasasdasd214124",
   "_score" : 1.0,
   "_source":
     {"A":1,"B":12,"C":122,"D":89,"E":120,"F":100,"G":2}
   }, {
   "_index" : "test",
   "_type" : "status",
   "_id" : "abc-2345-dadasasdasd214124",
   "_score" : 1.0,
   "_source":
     {"A":54,"B":171,"C":102,"D":0,"E":120,"F":11,"G":20}
   } , {
    "_index" : "test",
    "_type" : "status",
    "_id" : "abc-2345-dadasasdasd214124",
    "_score" : 1.0,
    "_source":
     {"A":94,"B":8,"C":155,"D":32,"E

注意:上面给出的 json 数据是示例数据。真正的 json 文件包含大量数据。请指教。谢谢

同时尝试确保 FileWriter 已关闭。

} finally {
  try { file.close(); inputStream.close(); } catch (Exception ignore) {}
}

您的程序可能正在退出,而某些数据仍处于缓冲状态且尚未写入文件。