0

I need write a very big file (about 100-150 mb) with Delphi. This file will be a CSV file or a SQL file (the user can choose it).

In the past I always use a TStringList object to write text files but in this case I have a very big file and I think this is not the best solution. I need a speed and low memory solution if possible.

Martin
  • 1,065
  • 1
  • 17
  • 36
  • 3
    That's a small file. What problems are you having right now? You might benefit from my buffered stream code. http://stackoverflow.com/questions/5639531/buffered-files-for-faster-disk-access/5639712#5639712 – David Heffernan Jan 25 '14 at 23:54
  • 1
    Using buffered streams would optimize file I/O, but would not address memory usage. The entire `TStringList` would still be in memory while the file is being written, unless you use sometime like `TStreamWriter` to write to the stream in smaller pieces to reduce memory usage. – Remy Lebeau Jan 26 '14 at 01:16
  • @Remy is right. Unless you need the entire file in memory, don't do it. But buffering I/O is still worthwhile for a file of this size. – David Heffernan Jan 26 '14 at 08:44

1 Answers1

3

Use a TFileStream and write your output data to it as you are creating the data. That way, you are writing to the file immediately and it grows accordingly, and you are not wasting memory.

If you are using Delphi 2009 or later, you can use the TStreamWriter class to wrap the TFileStream. TStreamWriter has Write() and WriteLine() methods for writing String and formatted data.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770