Home » VB.NET File Handling

VB.NET File Handling

by Online Tutorials Library

VB.NET File Handling

A file is a collection of data stored in computer memory with a specific name and a defined folder path. The term File Handling in VB.NET is used to perform various operations like create a file, read a file, write to the file, closing the file, and more. Furthermore, when a file is opened for reading and writing, a stream is created.

A stream is a sequence of bytes that passes data to a file to read or write. In VB.NET file handling, there are two types of stream such as Input Stream or Read Stream and Output Stream or Write Stream.

VB.NET I/O Classes

In VB.NET, we use the System.IO namespace, that has different classes to perform various input and output operations with files, such as the FileStream class used to perform any operation like opening a file, closing a file, deleting a files, reading from or writing to a file, etc.

The following table shows the I/O classes that are commonly used in VB.NET programming.

I/O Class Description
File It is used to perform some changes in files.
FileInfo It is used to perform any operation on files.
FileStream It is used to apply read or write operations on any location of a file.
BinaryReader As the name represents, a Binary reader is used to read primitive data in a binary stream.
BinaryWriter It is used to write the data in binary format.
Directory It is used to perform some changes or manipulating a directory structure.
DriveInfo It helps to provide the necessary information for the drive.
BuffredStream It is a temporary storage area for the collection of steam bytes.
MemoryStream It is used to access stored streaming data in memory.
StreamReader A StreamReader property is used to read characters from the stream byte.
Path It is used to perform operations on the path of a file.
StreamWriter A StreamWriter is used to write characters to a stream.
DirectoryInfo It is used to perform an operation on the directory.
StringReader It is used to read string from a string buffer.

FileStream Class

The FileStream class is provided by the System.IO namespace to read, write, close or create files in the file handling.

Syntax:

For example, we need to create a FileStream Object FS to read a file with named myfile.txt.

The following table is the description of FilesStream Class.

Parameter Description
FileMode The FileMode represents the various method for opening or creating a file. Following are the member of FileMode-
Append: It is used to open an existing file and put the cursor at the end of the file. And if the file is not existing, it creates a file.
Create: As the name defines, a create is uses to create a new file.
CreateNew: It specifies the OS to create a new file.
Open: It is used to open an existing file.
OpenOrCreate: It is used to open an existing file, and if the file is not existing, it creates a new file.
Truncate: It is used to open an existing file for shrinking its original size to zero bytes.
FileAccess It is used to perform any operation such as Read, ReadWrite, Delete and Write, etc.
FileShare A FileShare has the following members
Read: It permits to open a file for reading
Write: It permits you to open a file for writing.
None: It is used to reject the sharing of the current file.
Obj_name It represents the object name of the file.

Let’s create a program to understand the concept of FileStream in VB.NET program.

File_Prog.vb

Output:

VB.NET File Handling

VB.NET has some advanced concepts of file operation.

1. StreamReader and StreamWriter

The StreamReader class is used to read a text from a text file, and the StreamWrite file is used to write a text to a specified text file. These Stream classes are inherited from the Abstract base class stream, which represents a reader to read a series of characters, and a writer can write a series of characters.

Example of StreamReader Class

Let’s create the following example to understand the concept of StreamReader to read a text file named Myfile.txt, as shown below.

Myfile.txt

StReader.vb

Output:

VB.NET File Handling

Example of StreamWriter Class

Let’s create the following example to understand the concept of StreamWriter to write a text into a file Mytext.txt, as shown below.

StWriter.vb

Output:

VB.NET File Handling

Further, we can also check if the file “Mytext.txt” is created on the defined route “C:/Users/AMIT YADAV/Desktop/Mytext.txt” or not. And when we follow the defined path, it shows the following content in Mytext file.

VB.NET File Handling

Note: Only one stream can be opened at a time. Therefore, in the above example, we first wrote data to the file and then closed the stream. After that, we open the Read stream to read the text and then closed the stream

2. BinaryReader and BinaryWriter Classes

The BinaryReader and BinaryWriter classes are used with binary streams. Binary data is read and write using its internal binary format, and these binary data are not human readable. The BinaryReader class can be used to read binary data from a file, and the BinaryWriter file is used to write text to a specified binary file.

Example of BinaryWriter Class

Let’s create the following example to understand the concept of StreamWriter to write a text into a file Mytext.txt, as shown below.

BinWriter.vb

Output:

VB.NET File Handling

Example of BinaryReader Class

Let’s create the following example to understand the concept of BinaryReader to read the text into a file Mytext.txt.

BinReader.vb

Output:

VB.NET File Handling

3. FileInfo Class

The FileInfo class is used to get file properties such as file creation, copying, moving, name, and size. It also helps to create a FileStream object, and the FileInfo class is derived from the FileSystemInfo class.

Let’s create an example to understand the concept of the FileInfo class in VB.NET.

GetFilename.vb

Output:

VB.NET File Handling


Next TopicVB.NET Date & Time

You may also like