MattsBits
MattsBits - Brunel District

MattsBits

MattsBits RSS Feed - Subscribe Now!

22/04/2009 : How To Read And Write Text Files In VB.NET

Categories : | Microsoft .NET | Views : 354 | Email Link Print

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!")

Technorati Tags : | programming | vb | microsoft |

Author : Matt Hawkins  Last Edited By : Matt Hawkins  Permalink : How To Read And Write Text Files In VB.NET
 
 
 
PHP Powered  MySQL Powered  Valid XHTML 1.0  Valid CSS  Firefox - Take Back The Web  EUKHost - Recommended Webhosting Solutions

MattHawkins CMS v2.0 - Copyright 2009-2010