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

前端 未结 4 1086
心在旅途
心在旅途 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 05:08

    Don't try to read large files into memory. They don't fit. Find a way of processing the file a line at a time, or a record at a time, or a chunk at a time. I can't see any reason here why you can't do that.

    Calling File.exists() and File.createNewFile() immediately before constructing a FileWriter around the same File is a complete waste of time.

提交回复
热议问题