0

I want to read and display a very large file of text, how do I slice it and read it chunk by chunk to avoid using huge amounts of memory?

The idea is to read first 1mb for example, then convert to string and display, then read only the next 1mb, do the same...

It's important not to allocate the entire file in memory.

Vladislav
  • 1,392
  • 1
  • 12
  • 21
  • 1
    This is not a duplicate of "Read a file/URL line-by-line in Swift". An answer to this question leaves more room for alternative, more optimised implementations - which are far better suited than reading a file line by line via a file handle. – CouchDeveloper Apr 07 '16 at 09:49
  • @CouchDeveloper: Note that there are several answers in http://stackoverflow.com/questions/24581517/read-a-file-url-line-by-line-in-swift which do exactly what is asked here, namely reading data effectively in chunks. My answer does it "manually", other answers use the built-in buffering of stdio. But if you still disagree, I will reopen the question! – Martin R Apr 07 '16 at 18:17

0 Answers0