MattsBits
MattsBits

MattsBits

How To Read And Write Text Files In VB.NET  

by Matt Hawkins, 22/04/2009
Categories : Microsoft .NET

This example code shows how you can use the StreamReader class to read an write standard text files. The code below can be used as a basis for reading lines from one file, processing them and then writing them back out to another file.

I have used this code as a base for processing CSV, HTML and XML files.


On Error GoTo ErrorHandler

Dim oRead As System.IO.StreamReader
Dim oWrite As System.IO.StreamWriter

Dim strLine

Dim intLineCounter

intLineCounter = 0

oRead = IO.File.OpenText("input.txt")
oWrite = IO.File.CreateText("output.txt")

Do While oRead.Peek() <> -1

strLine = oRead.ReadLine

' Process or modify the strLine variable here
' before it is written to the output file

oWrite.WriteLine(strLine)

intTotalCounter = intTotalCounter + 1

Loop

oWrite.Close() ' Close output file
oRead.Close() ' Close input file

Exit Sub

ErrorHandler:

msgbox("There was an error!")

Author : Matt Hawkins  Last Edit By : Matt Hawkins
PHP Powered  MySQL Powered  Valid XHTML 1.0  Valid CSS  Firefox - Take Back The Web  EUKHost - Recommended Webhosting Solutions

MattHawkins CMS v3.0 - Copyright 2009-2022