Reading and writing to text file

mjsg

New member
Joined
Sep 19, 2003
Messages
2
Dear All,

I am a newbie to VB.Net. I trying to get my (VB.Net) app to read from a text file and manipulate the read data and write the output to another text file. I can do it using VB6 and VBA, but not in VB.Net. I have a few questions I need sorted out.

1) what is the diff between fileopen and streamreader?
2) how do I use streamreader to read a line of text with a series of variables demilited by comma and then separated in different variables without using the split function?
3) how do I use streamwriter to do the reverse ie write a series of variables to a line of text and separated by commas?
4) I have declared a number of variables as various types eg long, integer, single, string etc. I would get an error message if the variables are of different type to the value being read and more importantly if a value is blank ie field is blank in the input file it would also return an error message. How do I get around this?

Your help and suggestions are greatly appreciated.

TIA
 
1) FileOpen is the old non-.Net way of doing things and StreamReader is the correct way ;)
2)

Code:
Dim sr as new System.IO.StreamReader("C:\test.txt")
dim line as string
line = sr.ReadLine()
dim vals() as string = line.split(",")

3)
Code:
Dim swr as new System.IO.StreamWriter("C:\test.txt")
dim line as string
dim vals() as string = {"test","data","Only"}
line = line.join(vals,",")
sw.WriteLine(line)

4) bit more difficult but most data types have methods to support conversion i.e.
integer.Parse will parse a string into an integer
Long.ToString will convert to a string etc.
 

Similar threads

C
Replies
0
Views
63
Cas Raj
C
S
Replies
0
Views
57
sva0008
S
E
Replies
0
Views
242
Eldeeb92
E
Back
Top