编写Big JSON文件以避免OutOfMemory问题的最佳方法
首先,请注意今天是我与GSON
第一天。 我正在尝试使用GSON
库编写Json文件。 我在ArrayList
有成千上万的JsonObjects
。 写入Json文件时,它看起来应该与此类似。
[ { "hash_index": "00102x05h06l0aj0dw", "body": "Who's signing up for Obamacare?", "_type": "ArticleItem", "title": "Who's signing up for Obamacare? - Jan. 13, 2014", "source": "money.cnn.com", "primary_key": 0, "last_crawl_date": "2014-01-14", "url": "http://money.cnn.com/2014/01/13/news/economy/obamacare-enrollment/index.html" }, { "hash_index": "00102x05h06l0aj0dw0iz0kn0l@0t#0", "body": "Who's signing up for Obamacare?", "_type": "ArticleItem", "title": "Who's signing up for Obamacare? - Jan. 13, 2014", "source": "money.cnn.com", "primary_key": 1, "last_crawl_date": "2014-01-14", "url": "http://money.cnn.com/2014/01/13/news/economy/obamacare-enrollment/index.html" } ]
现在,我使用下面的代码编写JSOn。
private void writeNewJsonFile() throws IOException { System.out.println("Starting to write the JSON File"); //Add everything into a JSONArray JsonArray jsonArrayNew = new JsonArray(); for(int i=0;i<jsonObjectHolder.size();i++) { System.out.println("inside array"); jsonArrayNew.add(jsonObjectHolder.get(i)); } //Write it to the File /* File file= new File("items_Articles_4_1.json"); FileWriter fw = new FileWriter(file);; fw.write(jsonArrayNew.toString()); fw.flush(); fw.close();*/ System.out.println("outside array"); ByteArrayInputStream input = new ByteArrayInputStream(jsonArrayNew.toString().getBytes()); Long contentLength = Long.valueOf(jsonArrayNew.toString().getBytes().length); ObjectMetadata metaData = new ObjectMetadata(); metaData.setContentLength(contentLength); s3.putObject(outputBucket,outputFile,input,metaData); }
在这里,我将JsonArray
转换为String
并进行编写。 我担心这会很快崩溃Big Json数组并给我OutOfMemoryException
。 就像我使用GSON一部分阅读Json文件一样,是否有任何方法可以逐个编写Json文件,这可以避免OutOfMemoryException
问题?
我正在使用下一个代码:
WriteJsonArrayByParts write = new WriteJsonArrayByParts (fileNameTest, " "); write.writeStart(); for(Cache cache : listOfObjects()) { write.writeObject(cache, Cache.class); } write.writeEnd(); write.close();
…
public static class WriteJsonArrayByParts { Gson gson = new Gson(); JsonWriter writer; public WriteJsonArrayByParts(String fileNameWithPath, String indent) throws Exception { OutputStream os = new FileOutputStream(fileNameWithPath, false); BufferedOutputStream osb = new BufferedOutputStream(os, 8 * 1024); writer = new JsonWriter(new OutputStreamWriter(osb, StringUtil.UTF_8)); writer.setIndent(indent); } public void writeStart() throws IOException { writer.beginArray(); } @SuppressWarnings("unchecked") public void writeObject(T t, Class> resultClass) throws IOException { ((TypeAdapter
- Spring Boot亚马逊AWS S3存储桶文件下载 – 访问被拒绝
- 如何从用Java编写的AWS Lambda函数中读取S3文件?
- S3 Java客户端因“Content-Length delimited message body的过早结束”或“java.net.SocketException Socket closed”而失败了很多
- maven s3旅行车提供商
- 如何从S3下载GZip文件?
- 从S3下载文件> 3Gb失败,出现“SocketTimeoutException:Read timed out”
- AmazonS3,如何检查上传是否成功?
- 如何使用REST API和数据库扩展Java应用程序?
- amazon s3上传文件超时