Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming Lecture 6

Similar presentations


Presentation on theme: "Introduction to Programming Lecture 6"— Presentation transcript:

1 Introduction to Programming Lecture 6
Msury Mahunnah, Department of Informatics, Tallinn University of Technology

2 “Operations With Text Files”

3 Text Files Text files have an extension that ends with .txt
It is called a Sequential File

4 Working with files File handling in Visual Basic is based on System.IO namespace with a class library that supports string, character and file manipulation. These classes contain properties, methods and events for creating, copying, moving, and deleting files. The most commonly used classes are FileStream, BinaryReader, BinaryWriter, StreamReader and StreamWriter.

5 You can open a Text File for ...
Reading Writing Appending

6 Opening a Text File for Reading
Const fname$ = “C:\Users\Owner\Documents\test.txt“ Dim objReader As New System.IO.StreamReader(fname) After this sentence the file is not opened yet “As New” – This means “Create a New Object”. The type of the object is a StreamReader: System.IO.StreamReader

7 Opening a Text File for Reading
One of the StreamReader methods available for reading is “ReadToEnd” (reading whole file at once) This method read the whole text, right to the end. i.e. TextBox1.Text = objReader.ReadToEnd (After this sentence the file is opened and the text is read) It is imporant to close the StreamReader after reading (otherwise you’ll get error messages) objReader.Close()

8 ReadToEnd example Const fname$ = “C:\Users\Owner\Documents\test.txt" If System.IO.File.Exists(fname) = True Then Dim objReader As New System.IO.StreamReader(fname) richtextbox1.AppendText(objReader.ReadToEnd) objReader.Close() Else MsgBox(“File Does Not Exist”) End If

9 Reading Line by Line Method ReadToEnd is used for reading the whole file at once Method ReadLine for reading one line at a time To go through the whole file, use Do ... Loop statement with Peek method Peek signifies the end of the text file. Returns the next available character but does not consume it. If Peek returns -1 the reading sequence (process) is at the end of the file.

10 ReadLine example Const fname$ = “C:\Users\Owner\Documents\test.txt“ If System.IO.File.Exists(fname) = True Then Dim objReader As New System.IO.StreamReader(fname) Dim TextLine As String = “” Do While objReader.Peek() <> -1 TextLine = TextLine & objReader.ReadLine() & vbNewLine Loop richtextbox1.AppendText(TextLine) objReader.Close() Else MsgBox(“File Does Not Exist”) End If

11 Opening a Text File for Writing
Const fname$ = “C:\Users\Owner\Documents\test2.txt" Dim objWriter As New System.IO.StreamWriter(fname) After this sentence the file is not opened yet The type of the object is a StreamWriter: System.IO.StreamWriter

12 Writing into Text File Method Write add text into Text File.
VB insists that the file must exist before it can actually do something with it! objWriter.Write(“Hello world”) or objWriter.WriteLine(“Hello world”) - This sentence writes “Hello word” into file and goes to new line

13 Write example Const fname$ = “C:\Users\Owner\Documents\test.txt“ If System.IO.File.Exists(fname) = True Then Dim objWriter As New System.IO.StreamWriter(fname) objWriter.Write(“Hello world!”) objWriter.Close() MsgBox(“Text written to file!”) Else MsgBox(“File Does Not Exist”) End If

14 Creating a file if it doesn’t exist
Const fname$ = “C:\Users\Owner\Documents\test.txt“ Dim objWriter As New System.IO.StreamWriter(fname, False) objWriter.Write(“Hello world!”) objWriter.Close() MsgBox(“Text written to file!”) (Add a comma and False)

15 Opening a Text File for Appending
Const fname$ = “C:\Users\Owner\Documents\test.txt" If System.IO.File.Exists(fname) = True Then Dim objWriter As New System.IO.StreamWriter(fname, True) objWriter.Write(“Hello world!”) objWriter.Close() MsgBox(“Text written to file!”) Else MsgBox(“File Does Not Exist”) End If (Add a comma and True)

16 Reading from a file and writing into an array
Dim TextLine$ Dim seq As Object Dim t&=-1 Dim i&, V$() .... TextLine = objReader.ReadLine() seq = Strings.Split(TextLine, “ “) For i = Lbound(seq) To Ubound(seq) t+=1 Redim Preserve V(t) V(t) = seq(i) Next ...

17 All txt-files in certain folder
Dim files() As String files = System.IO.Directory.GetFiles(path, pattern) files = System.IO.Directory.GetFiles(“C:\Users\John\", "*.txt") MsgBox("Found " & files.Length & " txt-files") Dim file As String For Each file In files RTB.AppendText(file & vbNewLine) Next RTB - RichTextBox

18 Create and delete a directory
System.IO.Directory.CreateDirectory(path) System.IO.Directory.CreateDirectory(“C:\Users\John\folder1\folder2\”) Possibility to add so many subfolder as needed! System.IO.Directory.Delete(path) System.IO.Directory.Delete(“C:\Users\John\folder1\folder2\”)

19 Delete all txt-files in folder
Dim files() As String files = System.IO.Directory.GetFiles(“C:\Users\John", "*.txt") MsgBox("Found " & files.Length & " txt-files") Dim file As String For Each file In files System.IO.File.Delete(file) Next

20 Retrieving the Names of All Drives in the Computer
Dim drives() As String drives = System.IO.Directory.GetLogicalDrives Dim drive As String For Each drive In drives RTB.AppendText(drive & vbNewLine) Next RTB - RichTextBox


Download ppt "Introduction to Programming Lecture 6"

Similar presentations


Ads by Google