We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11
Working with text files
• The ability to open textfile and read contents
is a uselful process in programming. • Textfile may contain answers to questions,save some data associate to a program i.e errors. Open text file • We need a class object called StreamReader.it reads a stream of text.its an object found in the class called system.io. • To create a stream reader. • Dim filename=“c:\users\t.txt” • Dim objReader as New System.io.Streamreader(filename) • 1st-sets the variable to store the path. • Streamreader is set to a variable called objreader. • System is the main object.io is below it and stream reader is below i.o.the file to read is put between the quotes. Read File • You say.textbox1.text=objreader.readtoend • Create a new Project. • Add a textbox ad set its property to multiline. • Add a button and call it open. • Type • Dim filename As String = "C:\Documents and Settings\Mobile Dev\Desktop\flash.txt" • Dim objreader As New System.IO.StreamReader(filename) • TextBox2.Text = objreader.ReadToEnd • objreader.Close() Create a file • If System.IO.File.Exists("c:\testFile.txt") Then • 'shows message if testFile exist • MsgBox("File 'testFile' Exist ") • Else • 'create the file testFile.txt • System.IO.File.Create("c:\testFile.txt") • MsgBox("File 'testFile' created ") • End If Writing to file • Dim filename As String = "c:\testFile.txt" • If System.IO.File.Exists(filename) = True Then • Dim objwriter As New System.IO.StreamWriter(filename) • objwriter.WriteLine(TextBox2.Text) • objwriter.Close() • MessageBox.Show("wrote to file") • Else • MessageBox.Show("No such file") • End If Append to file • We add the word true next to the file name on the streamwriter object • Dim objWriter As New System.IO.StreamWriter( FILE_NAME, True ) Copy Files • Dim filename As String = "c:\testFile.txt" • Dim filename1 As String = "c:\testFiless.txt“//new textfile • If (System.IO.File.Exists(filename) = True) Then • System.IO.File.Copy(filename, filename1) • MessageBox.Show("file copied") • Else • MessageBox.Show("no file copied") • End If Move Files • Dim filename As String = "c:\testFiless.txt" • Dim filename1 As String = "c:\testFil.txt" • If (System.IO.File.Exists(filename) = True) Then • System.IO.File.Move(filename, filename1) • MessageBox.Show("file moved") • Else • MessageBox.Show("no file moved") • End If Error file • Dim path As String = "E:\TUTORIALS\Visual Studio\ CdManagement\error.txt" • If System.IO.File.Exists(path) Then • Dim objwriter As New System.IO.StreamWriter(path) • objwriter.WriteLine(Date.Now & ex.message) • objwriter.Close() • Else • System.IO.File.Create(path) • Dim objwriter As New System.IO.StreamWriter(path) • objwriter.WriteLine(Date.Now & ex.message) • objwriter.Close() • Standard one pupil asked his brother named Dennis Gathu to create for him an automated application that would help him with basic calculation such as subtract, add, multiply and