New to .NET? Wondering how to split contents using a delimiter? No problem, this latest article from Ajaykumar shows you just how easy reading the contents of a delimited file can be.
Reading a Delimited File Using ASP.Net and VB.Net - Example Code (Page 3 of 6 )
To read files using .Net we need to declare a namespace called SYSTEM.IO, which contains a set of classes and methods used for reading and writing files.
Let us now declare a namespace with the name of System.IO:
Next, we open the script tag, which will run on the server. We also specify our scripting language as vb in the tag:
<Script language="vb" runat="server">
Next, we shall think about where to write the function, we shall write coding when the page loads, and we shall declare the Subroutine, which fires when the page loads. Let us now declare the subroutine
Sub page_load (Sender As Object,e As EventArgs)
We need to specify a filename which is to be read – whether it is a text file or a . csv file, the filename has to be assigned to a variable. Let us now declare a variable named filetoread, which will be used to store the filename.
Dim filetoread as string
Before we assign the file name to the variable, we will be using a server variable, which is used to trace the path of the specified file. Using server.mappath we can trace the absolute path of the file specified.
filetoread=server.mappath("readtest.txt")
In the above code we map the path of the text file readtest.txt where the path is stored into the variable filetoread. Next we need to initiate the Stream Reader class, where we declare filestream as a stream reader.
Dim filestream as StreamReader
We need to assign a file to the streamreader in order to open that file:
filestream = File.Opentext(filetoread)
In the above code we use the Opentext method of the streamer and pass the variable filetoread – the variable filetoread contains the filepath.
While streamer reads the content, we now need a place to store the contents of the file. We will have to declare a variable named readcontents with data-type string, which will store the contents of the file:
Dim readcontents as String
Using the ReadToEnd method of the streamer we read the entire contents of the variable readcontents:
readcontents = fileStream.ReadToEnd()
Now we will have to declare a variable, which will store the delimiter. Let us declare the variable named as textdelimiter with the data-type string.
Dim textdelimiter as String
Next, we’ll assign the delimiter, which may be a letter, word, number etc. For this example we’ll assign the delimiter to be “geeyes”:
Textdelimiter = "geeyes"
In the above code you can change the value of the variable textdelimiter as you wish. For example, if you wish to change the delimiter to “#” your variable will look like this:
Textdelimiter = “#”
If you want to read a space delimited text file then you will need to provide a space between the double quotes of the variable as shown:
Textdelimiter = “ “
The value of the variable is mandatory and is very important, as the reading of the delimited file depends on the value that is stored in the variable.