I am not very sure about the structure of your data file. This will be easy to understand if you could provide a sample of your file.
The main reason for memory consumption is the way the file is read / iterated. As soon as something is read, it remains in memory. You must use either java.io.FileInputStream or org.apache.commons.io.FileUtils .
Here is sample code to iterate using java.io.FileInputStream
try ( FileInputStream inputStream = new FileInputStream("/tmp/sample.txt"); Scanner sc = new Scanner(inputStream, "UTF-8") ) { while (sc.hasNextLine()) { String line = sc.nextLine(); addBodyToDatabase(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
Here is sample code to iterate using org.apache.commons.io.FileUtils
File file = new File("/tmp/sample.txt"); LineIterator it = FileUtils.lineIterator(file, "UTF-8"); try { while (it.hasNext()) { String line = it.nextLine(); addBodyToDatabase(line); } } finally { LineIterator.closeQuietly(it); }
source share