I'm trying to read each file in a directory that contains 4000 json.gz files. I'm running out of heap space during execution. I'm unsure how to solve this.
File folder = new File(directoryPath);
String [] files = folder.list();
assert files != null;
for (String file: files) {
String filePath = directoryPath + "/" + file;
if (filePath.substring(filePath.length() - 2).equalsIgnoreCase("gz")) {
try (GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(filePath))) {
InputStreamReader reader = new InputStreamReader(gzipInputStream);
Object obj = jsonParser.parse(reader);
TextFileModel textFileModel = processObject(obj, controller);
textFileModelList.add(textFileModel);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
UPDATE:
I've tried a few of the suggestions but I'm still getting the same error
try (InputStreamReader reader = new InputStreamReader(new GZIPInputStream(new FileInputStream(filePath)))){
Object obj = jsonParser.parse(reader);
reader.close();
TextFileModel textFileModel = processObject(obj, controller);
obj = null;
textFileModelList.add(textFileModel);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
Error Message:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at org.json.simple.parser.Yylex.yylex(Yylex.java:668)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:92)
at com.sbl.Main.main(Main.java:45)