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

时间:2016-03-07 00:25:28

标签: java json rest filewriter

我使用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文件包含大量数据。请指教。感谢

1 个答案:

答案 0 :(得分:1)

还要确保FileWriter已关闭。

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

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