VB.Net - SPLessons

VB.Net File Handling

Home > Lesson > Chapter 23
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

VB.Net File Handling

VB.Net File Handling

shape Description

In the computer language a file can be described as gathered of data stored in a particular storage device, when user opens the storage device to read the data stream operation will be performed, where read operation and write operations will be performed in a file.The stream is a "Collection of Bytes". There are two types of streams in VB.Net.

System.IO Classes

shape Description

System.IO namespace is used to perform input and output operations. We have to create a FileStream Class to do any operations like opening a file, closing a file...etc..The following table shows you I/O Classes that are mostly used.
Class Description
File Used perform changes in files
FileInfo Used to perform any operation in files
FileStream Used to perform Read or Write Operation in the file
StreamReader Used to read characters in a stream
StreamWriter Used to write characters in a stream 
StringWriter Used to write characters in Sting Buffer
StringReader Used to read characters in Sting Buffer
Path Used to perform operations on the file which is in the particular path
BinaryReader Used perform read operation in a binary stream
BinaryWriter Used perform write operation in a binary stream
MemoryStream Used perform  operations on the stored data
Directory Used to perform changes in the directory
DirectoryInfo Used to perform any operations in the directory

shape Syntax

The following is the Syntax to create a File Stream Class. [vbnet]Dim Object_Name As FileStream = New FileStream("Filename",FileMode,FileAccess)[/vbnet] Following one is the Description table for the above syntax.
Fields Description
Filename Name of the file which you want to do operations
FileMode Operation Name i.e Open or Create..etc
FileAccess Mode of Operation i.e Read or Write or Delete...etc
Object_Name Name of the object

shape Example

Following is an example to understand the concept of streams. [vbnet]Dim FS As FileStream = New FileStream("Example.txt", FileMode.Open, FileAccess.Read)[/vbnet] The following table describes the fields in the above example.
Fields Description
FS Object_Name
FileMode.Open FileMode in Open Position
FileAccess.Read File Access in Read Operation
Example.txt Name of the file

shape Example

Following is an example to understand the concept. [vbnet] Imports System.IO Module Module1 Sub Main() Dim f1 As FileStream = New FileStream("test.dat", _ FileMode.OpenOrCreate, FileAccess.ReadWrite) Dim i As Integer For i = 0 To 20 f1.WriteByte(CByte(i)) Next i f1.Position = 0 For i = 0 To 20 Console.Write("{0} ", f1.ReadByte()) Next i f1.Close() Console.ReadKey() End Sub End Module [/vbnet] Output: Following is a result in the console.

Summary

shape Points

  • CByte is function that represents an expression.
  • Append and Truncate both are the file mode enumerators.
  • Inheritable is an enumerator of file mode.