Home > SQL Server > Read and Write to Text file – VB.Net

Read and Write to Text file – VB.Net


I created this snippet to save my search time in future. So, you can also make use of this if needed.

The main objective of this code is to read a text file line by line and write it into another file. I know, it looks silly. because, why do we need to read a file and write to others without doing anything. But, The actual purpose is to read a text file and do some operations like update or remove. Using this method I can keep my original file unchanged. does it make sense now?

Code snippet

Imports System.IO

Dim strLine As String
Dim objStreamReader As StreamReader
Dim objStreamWriter As StreamWriter

objStreamReader = New StreamReader(“C:\test\source.txt”)
objStreamWriter = New StreamWriter(“C:\test\destination.txt”)

strLine = objStreamReader.ReadLine

Do While Not strLine Is Nothing
objStreamWriter.WriteLine(strLine)
strLine = objStreamReader.ReadLine
Loop

‘Close the file.
objStreamWriter.Close()
objStreamReader.Close()

I know there are millions of ways to do something. But, I choose this method and if you have a new logic please share it in the comment section.

Reference

Advertisements
Categories: SQL Server
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: