1 / 20

Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων

Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων. Γιάννης Πετράκης. Text Files and VB .NET. 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. How to Open a Text File in VB .NET.

fbane
Télécharger la présentation

Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης

  2. Text Files and VB .NET • 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.

  3. How to Open a Text File in VB .NET • To open up a text file, you need to create something called a "StreamReader". This, as its name suggests, reads streams of text. The StreamReader is an object available to System.IO. You create a StreamReader like this: Dim FILE_NAME As String = "C:\test.txt" Dim objReader As System.IO.StreamReader objReader = New System.IO.StreamReader(FILE_NAME)

  4. Read To End • 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: TextBox1.Text = objReader.ReadToEnd • Now that objReader 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). • One of the Methods available to our new StreamReader variable is the ReadToEnd method. This will read the whole of your text, right to the end.

  5. Read To End • Start a new project • Add a textbox to your new form, and just leave it on the default Name of Textbox1 • Set its MultiLine 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 objReader As New System.IO.StreamReader(FILE_NAME) TextBox1.Text = objReader.ReadToEnd objReader.Close() • What happens if the file does not exist??

  6. Read To End • 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 ThenDim objReader As New System.IO.StreamReader(FILE_NAME)TextBox1.Text = objReader.ReadToEndobjReader.Close()ElseMsgBox("File Does Not Exist")End If

  7. Reading a Text File Line by Line • 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 ReadToEnd method, you can use the ReadLine method: • The ReadLine 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 TextLine As String If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME) Do While objReader.Peek() <> -1TextLine = TextLine & objReader.ReadLine() & vbNewLineLoop Textbox1.Text = TextLine Else MsgBox("File Does Not Exist") End If

  8. Reading a Text File Line by Line Exercise • Make the user choose the path of the file to read • 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 ListBox (one ListBox item for each array cell) • Show in a MessageBox the number of the line that contains the word “test” (if “test” does not exist within the file show an appropriate message)

  9. How to Write to a Text File in VB .NET • Instead of using the StreamReader we use the StreamWriter. The StreamWriter is used to write a stream of text to a file. • 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:\test2.txt" If System.IO.File.Exists(FILE_NAME) = True ThenDim objWriter As New System.IO.StreamWriter(FILE_NAME)objWriter.Write(TextBox1.Text)objWriter.Close()MsgBox("Text written to file")ElseMsgBox("File Does Not Exist")End If

  10. How to Write to a Text File in VB .NET • If you don't have to write the whole text at once,you can write line by line. In that case, select WriteLine(instead of Write) from the available properties and methods. Here's an example of how to use WriteLine: Dim FILE_NAME As String = "C:\test2.txt"Dim i As IntegerDim aryText(4) As String aryText(0) = "Mary WriteLine"aryText(1) = "Had"aryText(2) = "A"aryText(3) = "Little"aryText(4) = "One" Dim objWriter As New System.IO.StreamWriter(FILE_NAME) For i = 0 To 4objWriter.WriteLine(aryText(i))Next objWriter.Close()

  11. How to Write to a Text File in VB .NET Exercise • Make the user choose the path of the file to write • Add a ListBox, insert some items and write the ListBox items within a file(one line for each ListBox Item)

  12. Appending Text to a File in VB .NET • 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. • To append text to a file, you type a comma after your file name then type the word True: Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)

  13. Appending Text to a File in VB .NET Dim FILE_NAME As String = "C:\test.txt"Dim i As IntegerDim aryText(4) As String aryText(0) = "Mary WriteLine"aryText(1) = "Had"aryText(2) = "Another"aryText(3) = "Little"aryText(4) = "One" Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True) For i = 0 To 4objWriter.WriteLine(aryText(i))Next objWriter.Close()MsgBox("Text Appended to the File")

  14. How to Copy a File in VB .NET • You can also copy a file that you've created. This time, we don't need the StreamWriter or StreamReader of System.IO. We need the File object: System.IO.File • 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 FileToCopy As StringDim NewCopy As String FileToCopy = "C:\test.txt"NewCopy = "C:\NewTest.txt" If System.IO.File.Exists(FileToCopy) = True ThenSystem.IO.File.Copy(FileToCopy, NewCopy)MsgBox("File Copied")End If

  15. How to Move a File with VB .NET • This time, we use the Move method of System.IO.File. Here's some code: FileToMove = "C:\test.txt"MoveLocation = "C:\ TestFolder\test.txt" If System.IO.File.Exists(FileToMove) = True ThenSystem.IO.File.Move(FileToMove, MoveLocation)MsgBox("File Moved")End If

  16. How to Delete a File in VB .NET • 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 FileToDelete As String FileToDelete = "C:\testDelete.txt" If System.IO.File.Exists(FileToDelete) = True Then System.IO.File.Delete(FileToDelete)MsgBox("File Deleted") End If

  17. Subroutines in VB .NET • Private Sub AddNumbers(ByVal first2 As Integer, ByVal second2 As Integer)Dim answer As Integer answer = first2 + second2 MsgBox "The total is " & answer End Sub • Call AddNumbers(4, 5)

  18. ByVal and ByRef in VB .NET • The word ByVal 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. • ByRef 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.

  19. ByVal and ByRef in VB .NET • Add a new button. Double click the button and add the following code: Dim Number1 As Integer Number1 = 10Call IncrementVariable(Number1) MsgBox(Number1) Private Sub IncrementVariable(ByVal Number1 As Integer)Number1 = Number1 + 1End Sub • What is the value of Number1 after the subroutine called? • Change the parameter to the this: ByRef Number1 As Integer

  20. How to Create a Function in VB .NET • Private Function AddNumbers(ByVal first2 As Integer, ByVal second2 As Integer)As Integer Dim answer As Integer answer = first2 + second2 AddNumbers=answer End Function • Dim sum as Integer sum=AddNumbers(4, 5) • Instead of saying AddTwoNumbers = answer you can use the Return keyword. You use it like this: Return answer

More Related