FileStream class and StreamReader class in VB.NET
File Stream class used to read from, write to, open and to close file any location within a file system.Stream class is used to read from and to write character from the text file.
File Stream class used to read from, write to, open and to close file any location within a file system.
Dim FileObj As New FileStream ("Filename with location", FileMode Enumerator, FileAccess Enumerator, FileShare Enumerator)
FileMode Enumerator: Used to opening file.
- Append: Append method use to create a new file if does not exist, if exists place the cursor at the end of file.
- Create: Create method use to create anew file.
- CreateNew: CreateNew method use to create a new file and specifies to an operating system.
- Open: Open method use to open an existing file.
- Open or Create: This method specifies to the operating system that it should opens file if it exists, otherwise it should create a new file.
- Truncate: This method open an existing file. When opened the file should be truncated so that its size is Zero bytes.
FileAccess Enumerator : Used to read from and write to file or perform both operation together. It used three method read, write and readwrite.
FileShare Enumerator : File share allow that other FileStream constructor can have to same file. It used six method are inheritance, none, read, readwrite, write and delete.
Stream class is used to read from and to write character from the text file and it is an abstract method which support reading and writng bytes into it.
StreamReader used to read data only from a text file.
StreamReader class used method are listed below.
- Close: This method used to close the object of StreamReader class and release all resources associated with the StreamReader class.
- Peek: This method return the next available character but does not consume it.
- Read: This method reads the next consecutive character from the stream.
- ReadLine: This method read a line of characters from the current stream and returns data as string.
- Seek: This method allow the read and write position to be moved to any position within the file.
Open notepad and save with check.txt name.
Dim fr As New FileReadStreamClass()
Public Class FileReadStreamClass
Public Sub ReadData()
Dim f As New FileStream("D:\check.txt", FileMode.Open, FileAccess.Read, FileShare.None)
Dim s As New StreamReader(f)
Dim str As String = s.ReadLine()
While str IsNot Nothing
str = s.ReadLine()