OutOfMemoryError when trying to read/write from a huge text file

前端 未结 4 1088
心在旅途
心在旅途 2021-01-21 04:46

I\'m trying to read/write a huge text file. But when I try to do that I get the error:

Exception in thread \"main\" java.lang.OutOfMemoryError: Java heap space
          


        
4条回答
  •  佛祖请我去吃肉
    2021-01-21 04:58

    To read a huge file in Java you should either user java.util.scanner or apache commons LineIterator. Both approaches don't load whole file in memory and read file line by line. Am able to read file of size greater than 1gb using LineIterator. Please visit this link for more details http://www.baeldung.com/java-read-lines-large-file and example.

提交回复
热议问题