saving string array to disk

N

Nikolay Petrov

I have a string array, which I need to save to a file.
The array may grow to thousands of elements.
My question is what is the fastest way to save it as text file?
The StreamWriter accepts String as parameter, but converting the array
to a single string takes a lot of time (by using a For loop and
concatenating the string).
How this can be done faster?

P.S. Every element of string array should be in separate row of the
file. So the CrLf should be added to each element of array.
 
C

Cor Ligthert

Nikolay,

Why not use the writeline of the StreamWriter in a loop through the array,
including the flush and the close not more than 8 lines of code in my
opinion.

Cor
 
N

Nikolay Petrov

that will do the trick
thanks Cor
it wasn't obvious to me at fitst look
newbie things, what can i say ;-)
 
H

Herfried K. Wagner [MVP]

Nikolay Petrov said:
I have a string array, which I need to save to a file.
The array may grow to thousands of elements.
My question is what is the fastest way to save it as text file?

I don't know if it's the fastest way, but I prefer something like this:

\\\
' Write data to file.
FileOpen(1, "C:\Foo.txt", OpenMode.Output)
Dim astr() As String = {"Hello", "World", "Bla"}
For Each t As String In astr
PrintLine(1, t)
Next t
FileClose(1)

' Read data from file.
FileOpen(1, "C:\Foo.txt", OpenMode.Input)
Dim s As String
Do Until EOF(1)
LineInput(1, s)
MsgBox(s)
Loop
FileClose(1)
///

Alternatively you can loop through the array and use a 'StreamWriter''s
'WriteLine' method to add a line to the file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top