Text Files and VB NET n There is
Text Files and VB. NET n There is a very useful object in VB. NET called System. IO (the IO stands for Input and Output). You can use this object to read and write to text files. 2
How to Open a Text File in VB. NET n To open up a text file, you need to create something called a "Stream. Reader". This, as its name suggests, reads streams of text. The Stream. Reader is an object available to System. IO. You create a Stream. Reader like this: Dim FILE_NAME As String = "C: test. txt" Dim obj. Reader As System. IO. Stream. Reader obj. Reader = New System. IO. Stream. Reader(FILE_NAME) 3
Read To End n But this won't do you any good. We haven't actually opened the text file yet. We've just told VB where the text file is and what object to open it with. You do the opening like this: Text. Box 1. Text = obj. Reader. Read. To. End n Now that obj. Reader is an object variable, it has its own properties and methods available for use (in the same way that the textbox has a Text property). n One of the Methods available to our new Stream. Reader variable is the Read. To. End method. This will read the whole of your text, right to the end. 4
Read To End n Start a new project n Add a textbox to your new form, and just leave it on the default n n Name of Textbox 1 Set its Multi. Line property to True Add a Button to your form. Set the Text property of the button to “Read from File". Double click the button and add the following code for it: Dim FILE_NAME As String = "C: test. txt" Dim obj. Reader As New System. IO. Stream. Reader(FILE_NAME) Text. Box 1. Text = obj. Reader. Read. To. End obj. Reader. Close() What happens if the file does not exist? ? 5
Read To End n You can, though, test to see if the file exists. If it does, you can open it; if not, you can display an error message. Amend your code to this (the new lines are in bold, red): Dim FILE_NAME As String = "C: test. txt" If System. IO. File. Exists(FILE_NAME) = True Then Dim obj. Reader As New System. IO. Stream. Reader(FILE_NAME) Text. Box 1. Text = obj. Reader. Read. To. End obj. Reader. Close() Else Msg. Box("File Does Not Exist") End If 6
Reading a Text File Line by Line n Quite often, you don't want to read the whole file at once. You want to read it line by line. In which case, instead of using the Read. To. End method, you can use the Read. Line method: n The Read. Line method, as its name suggests, reads text one line at a time. In order to do this, though, you need to use a loop. You can then loop round each line and read it into a variable. Here's a coding example: Dim FILE_NAME As String = "C: test. txt" Dim Text. Line As String If System. IO. File. Exists(FILE_NAME) = True Then Dim obj. Reader As New System. IO. Stream. Reader(FILE_NAME) Do While obj. Reader. Peek() <> -1 Text. Line = Text. Line & obj. Reader. Read. Line() & vb. New. Line Loop Textbox 1. Text = Text. Line Else Msg. Box("File Does Not Exist") End If 7
Reading a Text File Line by Line Exercise n Make the user choose the path of the file to read n Store the text of the file within an array (one array cell for each line of the file) and show the content of the array in a List. Box (one List. Box item for each array cell) n Show in a Message. Box the number of the line that contains the word “test” (if “test” does not exist within the file show an appropriate message) 8
How to Write to a Text File in VB. NET n Instead of using the Stream. Reader we use the Stream. Writer. The Stream. Writer is used to write a stream of text to a file. n Add another Button to the form you've been working on. Set the Text property of the button to "Write to File". Double click your new button to open up the coding window. Add the following: Dim FILE_NAME As String = "C: test 2. txt" If System. IO. File. Exists(FILE_NAME) = True Then Dim obj. Writer As New System. IO. Stream. Writer(FILE_NAME) obj. Writer. Write(Text. Box 1. Text) obj. Writer. Close() Msg. Box("Text written to file") Else Msg. Box("File Does Not Exist") End If 9
How to Write to a Text File in VB. NET n If you don't have to write the whole text at once, you can write line by line. In that case, select Write. Line (instead of Write) from the available properties and methods. Here's an example of how to use Write. Line: Dim FILE_NAME As String = "C: test 2. txt" Dim i As Integer Dim ary. Text(4) As String ary. Text(0) = "Mary Write. Line" ary. Text(1) = "Had" ary. Text(2) = "A" ary. Text(3) = "Little" ary. Text(4) = "One" Dim obj. Writer As New System. IO. Stream. Writer(FILE_NAME) For i = 0 To 4 obj. Writer. Write. Line(ary. Text(i)) Next obj. Writer. Close() 10
How to Write to a Text File in VB. NET Exercise n Make the user choose the path of the file to write n Add a List. Box, insert some items and write the List. Box items within a file(one line for each List. Box Item) 11
Appending Text to a File in VB. NET n There will be times when you won't want to erase all the text from your file. You'll only want to add text to what you currently have. In which case you need to Append. n To append text to a file, you type a comma after your file name then type the word True: Dim obj. Writer As New System. IO. Stream. Writer(FILE_NAME, True) 12
Appending Text to a File in VB. NET Dim FILE_NAME As String = "C: test. txt" Dim i As Integer Dim ary. Text(4) As String ary. Text(0) = "Mary Write. Line" ary. Text(1) = "Had" ary. Text(2) = "Another" ary. Text(3) = "Little" ary. Text(4) = "One" Dim obj. Writer As New System. IO. Stream. Writer(FILE_NAME, True) For i = 0 To 4 obj. Writer. Write. Line(ary. Text(i)) Next obj. Writer. Close() Msg. Box("Text Appended to the File") 13
How to Copy a File in VB. NET n You can also copy a file that you've created. This time, we don't need the Stream. Writer or Stream. Reader of System. IO. We need the File object: System. IO. File n File has it's own properties and methods you can use. One of these is Copy. Here's some code that makes a copy of our test file Dim File. To. Copy As String Dim New. Copy As String File. To. Copy = "C: test. txt" New. Copy = "C: New. Test. txt" If System. IO. File. Exists(File. To. Copy) = True Then System. IO. File. Copy(File. To. Copy, New. Copy) Msg. Box("File Copied") End If 14
How to Move a File with VB. NET n This time, we use the Move method of System. IO. File. Here's some code: File. To. Move = "C: test. txt" Move. Location = "C: Test. Foldertest. txt" If System. IO. File. Exists(File. To. Move) = True Then System. IO. File. Move(File. To. Move, Move. Location) Msg. Box("File Moved") End If 15
How to Delete a File in VB. NET n To delete a file from your computer, you use the Delete method of System. IO. Here's some new code for you to try: Dim File. To. Delete As String File. To. Delete = "C: test. Delete. txt" If System. IO. File. Exists(File. To. Delete) = True Then System. IO. File. Delete(File. To. Delete) Msg. Box("File Deleted") End If 16
Subroutines in VB. NET n Private Sub Add. Numbers(By. Val first 2 As Integer, By. Val second 2 As Integer) Dim answer As Integer answer = first 2 + second 2 Msg. Box "The total is " & answer End Sub n Call Add. Numbers(4, 5) 17
By. Val and By. Ref in VB. NET n The word By. Val is short for "By Value". What it means is that you are passing a copy of a variable to your Subroutine. You can make changes to the copy and the original will not be altered. n By. Ref is the alternative. This is short for By Reference. This means that you are not handing over a copy of the original variable but pointing to the original variable. Let's see a coding example. 18
By. Val and By. Ref in VB. NET n n n Add a new button. Double click the button and add the following code: Dim Number 1 As Integer Number 1 = 10 Call Increment. Variable(Number 1) Msg. Box(Number 1) Private Sub Increment. Variable(By. Val Number 1 As Integer) Number 1 = Number 1 + 1 End Sub What is the value of Number 1 after the subroutine called? Change the parameter to the this: By. Ref Number 1 As Integer 19
How to Create a Function in VB. NET n Private Function Add. Numbers(By. Val first 2 As Integer, By. Val second 2 As Integer) As Integer Dim answer As Integer answer = first 2 + second 2 Add. Numbers=answer End Function n Dim sum as Integer sum=Add. Numbers(4, 5) n Instead of saying Add. Two. Numbers = answer you can use the Return keyword. You use it like this: Return answer 20
- Slides: 20