将JSON对象存储到文件时,我一直收到Java heap space
错误。输入文件为180 MB。
我正在创建一个JSON对象,它由JSON对象的JSON数组组成。在处理不那么大的文件时,我总是遇到这个错误 将这些对象存储到文件的最佳方法是什么?
public class ProcessData {
public static void createJson(String path) throws Exception
{
BufferedReader br = new BufferedReader(new FileReader(path));
FileWriter fw = new FileWriter("restaurants.json");
try
{
JSONObject storeObj = new JSONObject();
JSONArray restaurantArray = new JSONArray();
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null)
{
JSONObject obj = new JSONObject();
String[] vals = line.split("\\t");
obj.put("id", vals[0]);
String[] tempvals = vals[1].split("\\|");
String[] details = tempvals[0].split(",");
obj.put("name", details[0]);
sb.setLength(0);
sb.append(details[2]);
sb.append(", ");
sb.append(details[3]);
sb.append(", ");
sb.append(details[1]);
sb.append(", ");
sb.append(details[4]);
String address = sb.toString();
address.replace("\\s+", " ");
obj.put("address", address);
JSONArray arr = new JSONArray();
for (int i = 0; i < tempvals.length; ++i)
{
JSONObject objtemp = new JSONObject();
details = tempvals[i].split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
objtemp.put("inspectionDate", details[7]);
objtemp.put("code", details[9]);
objtemp.put("description", details[10]);
objtemp.put("score", details[12]);
objtemp.put("grade", details[13]);
objtemp.put("gradeDate", details[14]);
arr.add(objtemp);
}
obj.put("violationDetails", arr);
restaurantArray.add(obj);
}
storeObj.put("restaurants", restaurantArray);
fw.write(storeObj.toString());
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
fw.flush();
fw.close();
br.close();
}
}
public static void main(String args[]) throws Exception {
try{
createJson("output/part-r-00000");
}
catch(Exception e){
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
我已尝试从您的函数中删除克里特JSONObject部件并手动创建您的JSON字符串并直接将其写入文件而不会缓冲到您的对象中。
以下是您的功能的更新代码:
public static void createJson(String path) throws Exception
{
BufferedReader br = new BufferedReader(new FileReader(path));
FileWriter fw = new FileWriter("restaurants.json");
try
{
String line;
StringBuilder sb = new StringBuilder();
fw.write("{\"restaurants\":");
line = br.readLine();
while (line != null)
{
fw.write("[{");
String[] vals = line.split("\\t");
fw.write("{");
fw.write("\"id\":\""+vals[0]+"\",");
String[] tempvals = vals[1].split("\\|");
String[] details = tempvals[0].split(",");
fw.write("\"name\":\""+details[0]+"\",");
sb.setLength(0);
sb.append(details[2]);
sb.append(", ");
sb.append(details[3]);
sb.append(", ");
sb.append(details[1]);
sb.append(", ");
sb.append(details[4]);
String address = sb.toString();
address.replace("\\s+", " ");
fw.write("\"address\":\""+address+"\",");
fw.write("\"violationDetails\":[");
StringBuilder sbStr = new StringBuilder();
for (int i = 0; i < tempvals.length; ++i)
{
details = tempvals[i].split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
sbStr.append("{");
sbStr.append("\"inspectionDate\":\""+details[7]+"\",");
sbStr.append("\"code\":\""+details[9]+"\",");
sbStr.append("\"description\":\""+details[10]+"\",");
sbStr.append("\"score\":\""+details[12]+"\",");
sbStr.append("\"grade\":\""+details[13]+"\",");
sbStr.append("\"gradeDate\":\""+details[14]+"\",");
sbStr.append("},");
}
if(tempvals.length>0)
sbStr.substring(0, sbStr.length()-1);
fw.write(sbStr.toString()+"]");
line = br.readLine();
if(line != null)
fw.write("}],");
else
fw.write("}]");
}
fw.write("}");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
fw.flush();
fw.close();
br.close();
}
}
我还没有查看真实的输入文件数据,所以请检查您的正确数据。
我希望这能解决你的问题。
您文件中的JSON输出可能如下所示:
{ &#34;餐馆&#34;:[ { &#34; id&#34;:&#34; id&#34;, &#34; name&#34;:&#34; name&#34;, &#34;地址&#34;:&#34;地址&#34;, &#34;违规行为&#34;:[ { &#34; inspectionDate&#34;:&#34; details7&#34;, &#34;代码&#34;:&#34; details9&#34;, &#34;描述&#34;:&#34; details10&#34;, &#34;得分&#34;:&#34;详情12&#34;, &#34;成绩&#34;:&#34;详情13&#34;, &#34; gradeDate&#34;:&#34; details14&#34; } ] } ]}