Using streams how do I write and then read a set of variables

J

Just Me

Using streams how do I write and then read a set of variables?
For example, suppose I want to write into a text file:

string1,string2,string3

Then read them later.

Suppose I want to write and then read:
string1, integer1, double1

Can't I have each read/write use one line in the text file (probably comma
separated)?

I've been reading but can only find out how to write one thing on a line.

Thanks in advance
 
J

Jay B. Harlow [MVP - Outlook]

Just Me,
Are you using a TextWriter or a BinaryWriter to write to the Stream?

A BinaryWriter & BinaryReader prefix strings with their length, while other
types (Integers, Doubles & such) are fixed sizes so what you write is what
you read.

Dim writer As BinaryWriter
writer.Write(string1)
writer.Write(integer1)
writer.Write(double1)

Dim reader As BinaryReader
string1 = reader.ReadString()
integer1 = reader.ReadInt32()
double1 = reader.ReadDouble()


Unfortunately TextWriter simply writes text, with NO formatting information.
Which means that TextReader has no parsing abilities.

You would need to manually write commas between the fields with TextWriter,
then manually parse the commas when you read them back...

I don't have any link handy, I would think a FormattedReader &
FormattedWriter might be useful set of base classes to define, along with
derived versions for FormattedString & FormattedStream, unfortunately I do
not know of any predefined right now...

Hope this helps
Jay
 
J

Just Me

Jay B. Harlow said:
Just Me,
Are you using a TextWriter or a BinaryWriter to write to the Stream?

I don't know.

Actually in this instance I need to write/read many records of three
strings.

I'd like the file to be a .txt file

I think it would be simple to concat the strings with commas seperators
before write, and then use split to separate then after read. Agree?

If that is the approach how do I use streams to read/write?

Thanks
 
J

Jay B. Harlow [MVP - Outlook]

Just Me,
I'd like the file to be a .txt file
If you want a .txt file, then you need (should) use a TextWriter,
specifically StreamWriter.
I think it would be simple to concat the strings with commas seperators
before write, and then use split to separate then after read. Agree?
Rather then concat the strings, I normally write the three strings & the
seperators to the StreamWriter. (eliminating any potential for temporary
strings cluttering the GC)...

Something like:
Dim writer As StreamWriter
writer.Write(string1)
writer.Write(","c)
writer.Write(integer1)
writer.Write(","c)
writer.Write(double1)
writer.WriteLine()

Of course the above may be in a loop, especially if the variables are coming
from a DataTable or any object via reflection...

Alternatively I've used the format parameter on TextWriter.WriteLine to
format the variables, something like:

Const format As String = "{0}, {1}, {2}"
writer.WriteLine(format, string1, integer1, double1)

The format parameter is more useful for internationalization & including
other text in the line...
before write, and then use split to separate then after read. Agree?
I normally use String.Split to read the file, however you can have problems
when the fields being written include the field delimiter or quoted
strings... I have not worked out a RegEx to use with RegEx.Split to more
intelligently split the fields...

Hope this helps
Jay



Just Me said:
I don't know.

Actually in this instance I need to write/read many records of three
strings.

I'd like the file to be a .txt file

I think it would be simple to concat the strings with commas seperators
before write, and then use split to separate then after read. Agree?

If that is the approach how do I use streams to read/write?

Thanks
<<snip>>
 
J

Just Me

It definitely helps
Thanks

Jay B. Harlow said:
Just Me,
If you want a .txt file, then you need (should) use a TextWriter,
specifically StreamWriter.

Rather then concat the strings, I normally write the three strings & the
seperators to the StreamWriter. (eliminating any potential for temporary
strings cluttering the GC)...

Something like:
Dim writer As StreamWriter
writer.Write(string1)
writer.Write(","c)
writer.Write(integer1)
writer.Write(","c)
writer.Write(double1)
writer.WriteLine()

Of course the above may be in a loop, especially if the variables are
coming from a DataTable or any object via reflection...

Alternatively I've used the format parameter on TextWriter.WriteLine to
format the variables, something like:

Const format As String = "{0}, {1}, {2}"
writer.WriteLine(format, string1, integer1, double1)

The format parameter is more useful for internationalization & including
other text in the line...

I normally use String.Split to read the file, however you can have
problems when the fields being written include the field delimiter or
quoted strings... I have not worked out a RegEx to use with RegEx.Split to
more intelligently split the fields...

Hope this helps
Jay




<<snip>>
 

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