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.
  • 1522
 

File Stream class used to read from, write to, open and to close file any location within a file system.

Syntax

Dim FileObj As New FileStream ("Filename with location", FileMode Enumerator, FileAccess Enumerator, FileShare Enumerator)

 FileMode Enumerator: Used to opening file.
  1. Append: Append method use to create a new file if does not exist, if exists place the cursor at the end of file.
  2. Create: Create method use to create anew file.
  3. CreateNew: CreateNew method use to create a new file and specifies  to an operating system.
  4. Open: Open method use to open an existing file.
  5. 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.
  6. 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.

Reading_Random_Value_from_Stream.png
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.

  1. Close: This method used to close the object of StreamReader class and release all resources associated with the StreamReader class.
  2. Peek: This method return the next available character but does not consume it.
  3. Read: This method reads the next consecutive character from the stream.
  4. ReadLine: This method read a line of characters from the current stream and returns data as string.
  5. 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.

check.txt

2notepad.gif

Module.vb

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.IO
Module Module1
    Sub Main()
        Dim fr As New FileReadStreamClass()
         fr.ReadData()
        Console.ReadLine()
    End Sub
    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)
             s.BaseStream.Seek(0, SeekOrigin.Begin)
            Dim str As String = s.ReadLine()
            While str IsNot Nothing
                Console.WriteLine("{0}", str)
                str = s.ReadLine()
            End While
             s.Close()
             f.Close()
        End Sub
    End Class
End
Module

Output

1Output.gif

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.