Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

VB.Net - File processing


May 13, 2021 vb.net


Table of contents


Afileis is a collection of data stored on disks with specific names and directory paths. When a file is opened for reading or writing, it becomes stream.

The stream is basically a sequence of bytes through the communication path. T here are two main streams: the input stream and the output stream. The input stream is used to read data from the file (read operation) and the output stream is used to write to the file (write operation).


VB.Net class I/O

System.IO namespace has various classes for performing various operations on files, such as creating and deleting files, reading or writing files, closing files, and so on.

The following table shows System.IO commonly used non-abstract classes in the namespace:

Class I/O Describe
BinaryReader Read the basic data of the binary stream.
BinaryWriter Write the original data in binary format.
BufferedStream Temporary storage for byte streams.
Directory A directory structure that helps to manipulate.
DirectoryInfo Used to operate on a directory.
DriveInfo provides information about the drive.
File Helps with files.
FileInfo Used to perform actions on a file.
FileStream Used to read and write anywhere in the file.
MemoryStream Random access to data stored in memory streaming.
Path The operation of the path information is performed.
StreamReader Used to read characters from a byte stream.
StreamWriter Used to write to a character stream.
StringReader Used to read from a string buffer.
StringWriter Used to write to string buffers.


FileStream class

System.IO fileStream class in the namespace helps you read, write, and close files. This class is derived from the abstract class Stream.

You need to create a FileStream object to create a new file or open an existing file. The syntax for creating a FileStream object is as follows:

Dim <object_name> As FileStream = New FileStream(<file_name>, <FileMode Enumerator>, <FileAccess Enumerator>, <FileShare Enumerator>)


For example, to create a FileStream object read file namedsample .txt:

Dim f1 As FileStream = New FileStream("sample.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)


Parameters Describe
FileMode

FileModeenumerator defines various ways to open a file. T he members of the FileMode enumerator are:

  • Append: It opens an existing file and places the cursor at the end of the file, or creates a file if the file does not exist.

  • Create: Create a new file.

  • CreateNew: It specifies that the operating system should create a new file.

  • Open: It opens an existing file.

  • OpenOrCreate: It specifies that the operating system should open a file if it exists, otherwise a new file should be created.

  • Truncate: It opens an existing file and truncates its size to zero bytes.

FileAccess

FileAccessenumerators has members: Read, ReadWrite and Write.

FileShare

FileShareenumerators has the following members:

  • Inheritable: It allows a file handle to pass inheritance sub-processes

  • None: It rejects the sharing of the current file

  • Read: It can open a file for reading

  • ReadWrite: It allows open files to be read and written

  • Write: It allows you to open a write file


Example:

The following program demonstrates using the FileStream class:

Imports System.IO
Module fileProg
   Sub Main()
      Dim f1 As FileStream = New FileStream("sample.txt", _
              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


When the above code is compiled and executed, it produces the following results:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1


Vb. A dvanced file operations in Net

The example above provides VB.Net file operations in the article. However, in order to take System.IO of these classes, you need to know the common properties and methods of those classes.

We'll discuss these classes and what they do in the following sections. Please click on the link provided to get each section:

Topics and descriptions

Reading from and Writing into Text files

It involves reading from and writing into text files. The StreamReader and StreamWriter classes help to accomplish it.

It involves reading and writing from a text file. TheStream Reader and StreamWriterclasses helped with it.

Reading from and Writing into Binary files

It involves reading from and writing into binary files. The BinaryReader and BinaryWriter classes help to accomplish this.

It involves reading and writing from a binary file. Binary Reader and BinaryWriterclasses help with this task.

Manipulating the Windows file system

It gives a VB.Net programmer the ability to browse and locate Windows files and directories.

It gives VB.Net programmers the ability to browse and locate Windows files and directories.