Rajesh’s Weblog

Just another WordPress.com weblog

Use FileStream to store and read file : VB.net

Posted by arajesh on April 9, 2008

Imports System
Imports System.IO

Public Class MainClass

Shared Sub Main()

Dim i As Integer
Dim theBytes(255) As Byte
For i = 0 To 255
theBytes(i) = CByte(i)
Next
Dim myFileStream As FileStream
Try
myFileStream = New FileStream(“test.txt”, FileMode.OpenOrCreate, FileAccess.Write)
myFileStream.Write(theBytes, 0, 256)
Finally
If Not (myFileStream Is Nothing) Then myFileStream.Close()
End Try

Dim theFile As FileStream
Try
theFile = New FileStream(“test.txt”,FileMode.Open, FileAccess.Read)
For i = 0 To (theFile.Length – 1)
Console.Write(theFile.ReadByte)
Next
Catch e As Exception
Throw e
Finally
If Not (theFile Is Nothing) Then theFile.Close()
End Try
End Sub

End Class

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>