VB.NET tutorial: File Stream-Random Access File

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

Imports System.IO
Module Module1
    Private index As Integer
    Sub Main()
        'display all records
        Dim confirm As Char
        addrecord() ' add a new record
        confirm = Console.ReadLine
        If confirm = "y" Then
            GoTo aa 'continue add new record
        End If
    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
    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
                '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
            Catch ex As Exception
            End Try
        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
                '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
        End Sub
    End Class
    Class Student
        Private stnumber As String
        Private stname As String
        Private recordsize As Integer
        Public Property st_number() As String
                Return stnumber
            End Get
            Set(ByVal value As String)
                stnumber = value
            End Set
        End Property
        Public Property st_name() As String
                Return stname
            End Get
            Set(ByVal value As String)
                stname = value
            End Set
        End Property
        Public ReadOnly Property size()
                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



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.