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.

2
  • 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. Commented Apr 7, 2016 at 9:49
  • @CouchDeveloper: Note that there are several answers in stackoverflow.com/questions/24581517/… 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! Commented Apr 7, 2016 at 18:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.