Read and Write Text Files With Visual Basic
Read and Write Text Files With Visual Basic
Read and Write Text Files With Visual Basic
Before we jump into working with text files, we need to create a new file or open an
existing one. That requires the System.IO.File class. This class contains methods for
many common file operations, including copying, deleting, file attribute manipulation,
and file existence. For our text file work, we will use the CreateText and OpenText
methods.
CreateText
True to its name, the CreateText method creates a text file and returns a
System.IO.StreamWriter object. With the StreamWriter object, you can then write to the
file. The following code demonstrates how to create a text file:
Dim oFile as System.IO.File
Dim oWrite as System.IO.StreamWriter
oWrite = oFile.CreateText(“C:\sample.txt”)
OpenText
The OpenText method opens an existing text file for reading and returns a
System.IO.StreamReader object. With the StreamReader object, you can then read the
file. Let’s see how to open a text file for reading:
Dim oFile as System.IO.File
Dim oRead as System.IO.StreamReader
oRead = oFile.OpenText(“C:\sample.txt”)
The overloaded method used in these examples accepts a string to be formatted and then
a parameter array of values to be used in the formatted string. Let’s look at both lines
more carefully.
The first line writes a header for our report. Notice the first string in this line is {0,10}
{1,10}{2,25}. Each curly brace set consists of two numbers. The first number is the index
of the item to be displayed in the parameter array. (Notice that the parameter array is zero
based.) The second number represents the size of the field in which the parameter will be
printed. Alignment of the field can also be defined; positive values are left aligned and
negative values are right aligned.
The second line demonstrates how to format values of various data types. The first field
is defined as {0,10:dd MMMM}. This will output today’s date (retrieved using the Now()
function) in the format 02 July. The second field will output the current time formatted as
02:15 PM. The third field will format the value 13455.33 into the currency format as
defined on the local machine. So if the local machine were set for U.S. Dollars, the value
would be formatted as $13,455.33.
Line-by-line
Reading a text file line-by-line is straightforward. We can read each line with a ReadLine
method. To determine whether we have reached the end of the file, we call the Peek
method of the StreamReader object. The Peek method reads the next character in the file
without changing the place that we are currently reading. If we have reached the end of
the file, Peek returns -1. Listing B provides an example for reading a file line-by-line
until the end of the file.
An entire file
You can also read an entire text file from the current position to the end of the file by
using the ReadToEnd method, as shown in the following code snippet:
Dim EntireFile as String
oRead = oFile.OpenText(“C:\sample.txt”)
EntireFile = oRead.ReadToEnd()
This example reads the file into the variable EntireFile. Since reading an entire file can
mean reading a large amount of data, be sure that the string can handle that much data.
Listing B
oRead = oFile.OpenText(“C:\sample.txt”)
End While
oRead.Close()
Listing C
Dim intSingleChar as Integer
oRead = oFile.OpenText(“C:\sample.txt”)
intSingleChar = oRead.Read()
cSingleChar = Chr(intSingleChar)
End While
http://articles.techrepublic.com.com/5100-10878_11-1045309.html#
oWrite.Close()
End Sub
oRead = oFile.OpenText("C:\Primer.txt")
Label9.Text = oRead.ReadLine()
Label10.Text = oRead.ReadLine()
TextBox1.Text = oRead.ReadLine()
oRead.Close()
' kod broji koliko se nalazi reci u textbox1, razmak uzima kao
jednu rec
Dim i, CountWord As Integer
For i = 1 To Len(TextBox1.Text)
If Mid(TextBox1.Text, Len(TextBox1.Text) - (i - 1), 1) <> "
" And i <> Len(TextBox1.Text) Then
Else
CountWord = CountWord + 1
End If
Next
MsgBox("There are " & CountWord & " words", MsgBoxStyle.OkOnly)
cSingleChar = Chr(intSingleChar)
End While
'Deljenje stringa na dva dela radi!!!
' We want to split this input string
Dim s As String = TextBox1.Text