VB.NET File HandlingA 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 ClassesIn 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.
FileStream ClassThe 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.
Let's create a program to understand the concept of FileStream in VB.NET program. File_Prog.vb Output: ![]() 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: ![]() 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: ![]() 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. ![]() 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 stream2. 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: ![]() 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: ![]() 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: ![]()
Next TopicVB.NET Date & Time
|