
File Handling in Visual Basic: Operations, Methods, and Examples
Learn about file handling in Visual Basic, including operations with text files, working with StreamReader and StreamWriter classes, opening files for reading, and reading files line by line. Discover how to manipulate files in VB using System.IO namespace for efficient file handling.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Introduction to Programming Lecture 6 Msury Mahunnah, Department of Informatics, Tallinn University of Technology
Text Files Text files have an extension that ends with .txt It is called a Sequential File
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 BinaryReader, StreamWriter. most commonly used classes StreamReader are FileStream, BinaryWriter, and
You can open a Text File for ... 1. Reading 2. Writing 3. Appending
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
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()
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
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.
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
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
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
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
Creating a file if it doesnt 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)
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 (Add a comma and True) MsgBox( File Does Not Exist ) End If
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 ...
All txt-files in certain folder Dim files() As String files = System.IO.Directory.GetFiles(path, pattern) 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 RTB.AppendText(file & vbNewLine) Next RTB - RichTextBox
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\ )
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 ForEach file In files System.IO.File.Delete(file) Next
Retrieving the Names of All Drives in the Computer Dim drives() As String drives = System.IO.Directory.GetLogicalDrives Dim drive As String ForEach drive In drives RTB.AppendText(drive & vbNewLine) Next RTB - RichTextBox