May 13, 2021 vb.net
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.
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.
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. |
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:
|
FileAccess |
FileAccessenumerators has members: Read, ReadWrite and Write. |
FileShare |
FileShareenumerators has the following members:
|
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
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.
|