VB.NET File Stream

Random Access File

In some situations, the programs require to access a particular record immediately. The records in random-access file can be accessed directly and quickly without searching through a large number of records as in the case of sequential-access file. Random-access file is used in instant access systems such as banking system, sale system, air-line reservation system…etc.

Writing to and Reading from a Random-Access file


Example:
Imports System.IO
Module Module1
    Private index As Integer
    Sub Main()
        'display all records
        displayrecords()
        Dim confirm As Char
 
aa:
        addrecord() ' add a new record
        Console.Write("Contitue?y/n:")
        confirm = Console.ReadLine
        If confirm = "y" Then
            GoTo aa 'continue add new record
        End If
 
        Console.ReadLine()
    End Sub
    Sub addrecord()
        Dim stu As New Student
        Dim f As New RandomFile
        Console.Write("Enter student number:")
        stu.st_number = Console.ReadLine
        Console.Write("Enter student name:")
        stu.st_name = Console.ReadLine
        f.writeToFile("D:\Student.dat", stu, index, stu.size)
        index += 1 'update index
 
    End Sub
    Sub displayrecords()
        Dim f As New RandomFile
        f.readFromFile("D:\Student.dat")
    End Sub
 
    Class RandomFile
        Public Sub writeToFile(ByVal filename As String, ByVal obj As Student, ByRef pos As Integer, ByVal size As Integer)
            Dim fout As FileStream
            Dim bw As BinaryWriter
 
            Try
                'open file to append data
                fout = New FileStream(filename, FileMode.Append, FileAccess.Write)
                bw = New BinaryWriter(fout)
                'set file position where to write data
                fout.Position = pos * size
                bw.Write(obj.st_number)
                bw.Write(obj.st_name)
 
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            fout.Close()
            bw.Close()
        End Sub
        Public Sub readFromFile(ByVal filename As String)
            Dim fn As FileStream
            Dim br As BinaryReader
            Dim stu As New Student
            Dim currentrecord As Integer = 0
            Try
                'open file to read data
                fn = New FileStream(filename, FileMode.Open, FileAccess.Read)
 
                br = New BinaryReader(fn)
                'read first record
                fn.Seek(0, 0)
                stu.st_number = br.ReadString
                stu.st_name = br.ReadString
                Console.WriteLine(stu.st_number & vbTab & stu.st_name)
                'read next record
                While stu.st_number <> ""

 
                    currentrecord += 1 'update currentrecord position
                    fn.Seek(currentrecord * stu.size, 0)
                    stu.st_number = br.ReadString
                    stu.st_name = br.ReadString
                    Console.WriteLine(stu.st_number & vbTab & stu.st_name)
 
                End While
 
            Catch ex As Exception
 
 
            End Try
            'update index to the current position
            index = currentrecord
            'close file
            fn.Close()
            br.Close()
 
        End Sub
    End Class
 
    Class Student
        Private stnumber As String
        Private stname As String
        Private recordsize As Integer
 
        Public Property st_number() As String
            Get
                Return stnumber
            End Get
            Set(ByVal value As String)
                stnumber = value
            End Set
        End Property
        Public Property st_name() As String
            Get
                Return stname
            End Get
            Set(ByVal value As String)
                stname = value
            End Set
        End Property
        Public ReadOnly Property size()
            Get
                Return calculatesize()
            End Get
 
        End Property
        Private Function calculatesize()
            recordsize = 2 * 15 + 2 * 20 '2 bytes of each char; max length of stnumber=15, max length of stname=20
            Return (recordsize)
        End Function
    End Class
 
End Module


HTML Comment Box is loading comments...



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.