📜  VB.Net-文件处理

📅  最后修改于: 2020-11-19 08:57:42             🧑  作者: Mango


文件是具有特定名称和目录路径的存储在磁盘中的数据的集合。打开文件进行读取或写入时,它成为

流基本上是通过通信路径的字节序列。主要有两个流:输入流输出流输入流用于从文件读取数据(读取操作),输出流用于写入文件(写入操作)。

VB.Net I / O类

System.IO命名空间具有各种类,这些类用于对文件执行各种操作,例如创建和删除文件,从文件读取或写入文件,关闭文件等。

下表显示了System.IO命名空间中一些常用的非抽象类-

I/O Class Description
BinaryReader Reads primitive data from a binary stream.
BinaryWriter Writes primitive data in binary format.
BufferedStream A temporary storage for a stream of bytes.
Directory Helps in manipulating a directory structure.
DirectoryInfo Used for performing operations on directories.
DriveInfo Provides information for the drives.
File Helps in manipulating files.
FileInfo Used for performing operations on files.
FileStream Used to read from and write to any location in a file.
MemoryStream Used for random access of streamed data stored in memory.
Path Performs operations on path information.
StreamReader Used for reading characters from a byte stream.
StreamWriter Is used for writing characters to a stream.
StringReader Is used for reading from a string buffer.
StringWriter Is used for writing into a string buffer.

FileStream类

System.IO命名空间中的FileStream类有助于读取,写入和关闭文件。该类派生自抽象类Stream。

您需要创建一个FileStream对象来创建一个新文件或打开一个现有文件。创建FileStream对象的语法如下-

Dim  As FileStream = New FileStream(, , , )

例如,创建FileStream对象F来读取名为sample.txt的文件-

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

The FileMode enumerator defines various methods for opening files. The members of the FileMode enumerator are −

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

  • Create − It creates a new file.

  • CreateNew − It specifies to the operating system that it should create a new file.

  • Open − It opens an existing file.

  • OpenOrCreate − It specifies to the operating system that it should open a file if it exists, otherwise it should create a new file.

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

FileAccess

FileAccess enumerators have members: Read, ReadWrite and Write.

FileShare

FileShare enumerators have the following members −

  • Inheritable − It allows a file handle to pass inheritance to the child processes

  • None − It declines sharing of the current file

  • Read − It allows opening the file for reading

  • ReadWrite − It allows opening the file for reading and writing

  • Write − It allows opening the file for writing

以下程序演示了FileStream类的用法-

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

编译并执行上述代码后,将产生以下结果-

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

VB.Net中的高级文件操作

前面的示例在VB.Net中提供了简单的文件操作。但是,要利用System.IO类的强大功能,您需要了解这些类的常用属性和方法。

在以下各节中,我们将讨论这些类及其执行的操作。请单击提供的链接以转到各个部分-

Sr.No. Topic and Description
1

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.

2

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.

3

Manipulating the Windows file system

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