SEM2: VISUAL BASIC II
PRESENTATION
BY
DR. FUSEINI JIBREEL
TEXT FILES
MODULE 1: TEXT FILES
A text file is a computer file that is
a kind of non-executable, digital
file and has only text.
It can have numbers, letters,
symbols, and/or a combination but
does not contain special
formatting such as italic text, bold
text, underline text, pictures
MODULE 1: TEXT FILES
MODULE 1: TEXT FILES
MODULE 1: TEXT FILES
A text file identifies with the .txt file
extension in Microsoft Windows computer. It
is also called ASCII files or flat files and is
used to store structured and standard textual
data or information, which can be readable by
humans.
MODULE 1: TEXT FILES
Also, these files are commonly
used to store things that can
be easily opened with any text
editor or written to with a
simple script.
Examples of usage of the text
files might include
storing step-by-step instructions
for how to use something,
a place to hold temporary
information, or
logs generated by a program
Why do we ask for text files?
They are simpler and faster to create
they are smaller so your zip files are smaller
and take up less room on our system
they are more portable - a machine running
just about any operating system can read them
they are easier to check for plagiarism
SYSTEM INPUT AND OUT
There is a very useful object in
VB.NET called System.IO (the IO
stands for Input and Output). This
object can be used to read and write to
text files.
VB.Net I/O Classes
How to create a Text
File in VB .NET
CREATING A TEXT FILE USING
VB.NET
The following program is designed to create a Text File in
VB.NET
1. Dim FileName As String
2. FileName = "C:\Users\Dr Jibreel\Documents\
Forgive.txt“
3. System.IO.File.Create(FileName)
4. MsgBox(" the file is created at the
specified location")
How to Write to a
Text File in
VB .NET
Practical 1
1.Dim FileName As String
2. FileName = "C:\Users\DrJibreel\Documents\
Forgive.txt"
3. Dim objwriter As New System.IO.StreamWriter(FileName)
4. objwriter.Write(TextBox1.Text)
5. objwriter.Close()
6. MsgBox("the text has been written in your named file")
7.Run your programme, and then click your button.
Testing for the
existence of
the File
Dim FILE_NAME As String = "C:\Users\DrJibreel\Documents\
Forgive.txt"
If System.IO.File.Exists(FILE_NAME) = False Then
MessageBox.Show("File does not Exist")
Else
MsgBox(" file exist")
End If
How to Open a Text
File in VB .NET
Dim FILE_NAME As String = "C:\Users\Dr
Jibreel\Documents\Forgive.txt"
Dim objReader As New
System.IO.StreamReader( FILE_NAME )
TextBox1.Text = objReader.ReadToEnd
objReader.Close()
How to stop overwriting on
the existing messages in the file
Dim FILE_NAME As String = "C:\Users\Prof\Documents\good3.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
objWriter.Write(TextBox1.Text)
objWriter.Close()
MessageBox.Show("Text written to file")
Else
MessageBox.Show("File Does Not Exist")
End If