VBMAN.FileEx - 高级文件操作对象
概述
VBMAN.FileEx 提供了高级文件操作功能,基于 VB 原生 Open 语句实现,支持多种编码格式、灵活的数据读写方式,以及缓冲区操作。
核心特性
- 多编码支持: UTF-8、ANSI、UTF-16LE 自动识别
- 灵活模式: 只读、只写、读写模式
- 缓冲区操作: 支持字节数组、Base64、Hex 等多种格式
- 链式调用: 流畅的 API 设计
- 大文件支持: 支持最大 2GB 文件操作
属性
| 属性 | 类型 | 说明 |
|---|---|---|
IsOpen | Boolean | 文件是否已打开 |
FilePath | String | 当前打开的文件路径 |
LastError | String | 最后错误信息 |
FileSize | Currency | 文件大小(字节) |
BufferSize | Long | 当前缓冲区字节数 |
方法
文件打开/关闭
OpenFile
打开文件
vb
Public Function OpenFile(ByVal FilePath As String, Optional ByVal AccessMode As String = "RW") As cFileEx参数:
FilePath- 文件路径AccessMode- 访问模式:"R"=只读, "W"=只写, "RW"=读写(默认)
示例:
vb
' 只读模式
VBMAN.FileEx.OpenFile "C:\\data.txt", "R"
' 读写模式(默认)
VBMAN.FileEx.OpenFile "C:\\data.txt"
' 链式调用
VBMAN.FileEx.OpenFile("C:\\data.txt").ReadData.ReturnTextCloseFile
关闭文件
vb
Public Sub CloseFile()示例:
vb
VBMAN.FileEx.OpenFile "C:\\data.txt", "R"
' ... 操作 ...
VBMAN.FileEx.CloseFile缓冲区设置
ClearBuffer
清空内部缓冲区
vb
Public Function ClearBuffer() As cFileExSetBuffer
用字节数组填充缓冲区
vb
Public Function SetBuffer(Data() As Byte) As cFileEx示例:
vb
Dim bytes(0 To 3) As Byte
bytes(0) = &H41: bytes(1) = &H42
bytes(2) = &H43: bytes(3) = &H44
VBMAN.FileEx.SetBuffer bytesSetBufferText
用字符串填充缓冲区(指定编码)
vb
Public Function SetBufferText(ByVal Text As String, Optional ByVal CharSet As String = "UTF-8") As cFileEx示例:
vb
VBMAN.FileEx.SetBufferText "Hello 世界", "UTF-8"SetBufferBase64
用 Base64 字符串填充缓冲区
vb
Public Function SetBufferBase64(ByVal Base64String As String) As cFileEx示例:
vb
VBMAN.FileEx.SetBufferBase64 "SGVsbG8gV29ybGQ="SetBufferHex
用 Hex 字符串填充缓冲区
vb
Public Function SetBufferHex(ByVal HexString As String, Optional ByVal Separator As String = " ") As cFileEx示例:
vb
VBMAN.FileEx.SetBufferHex "48 65 6C 6C 6F"
VBMAN.FileEx.SetBufferHex "48656C6C6F"SetBufferBinString
用二进制字符串填充缓冲区
vb
Public Function SetBufferBinString(ByVal BinString As String, Optional ByVal Separator As String = " ") As cFileEx示例:
vb
VBMAN.FileEx.SetBufferBinString "01001000 01100101"AppendBuffer
追加字节到缓冲区
vb
Public Function AppendBuffer(Data() As Byte) As cFileEx数据读取
ReadData
读取文件数据到内部缓冲区
vb
Public Function ReadData(Optional ByVal StartPos As Long = -1, Optional ByVal EndPos As Long = -1) As cFileEx参数:
StartPos- 起始位置(1-based,-1=文件开头)EndPos- 结束位置(1-based,-1=文件末尾)
示例:
vb
' 读取整个文件
VBMAN.FileEx.OpenFile("C:\\data.txt").ReadData.ReturnText
' 读取指定范围
VBMAN.FileEx.OpenFile "C:\\data.txt", "R"
VBMAN.FileEx.ReadData 1, 100 ' 读取前100字节数据返回
ReturnText
返回缓冲区内容为文本
vb
Public Function ReturnText(Optional ByVal CharSet As String = "UTF-8") As String示例:
vb
Dim text As String
text = VBMAN.FileEx.OpenFile("C:\\utf8.txt").ReadData.ReturnText("UTF-8")ReturnBytes
返回缓冲区内容为字节数组
vb
Public Function ReturnBytes() As Byte()示例:
vb
Dim bytes() As Byte
bytes = VBMAN.FileEx.OpenFile("C:\\data.bin").ReadData.ReturnBytesReturnBase64
返回缓冲区内容为 Base64 字符串
vb
Public Function ReturnBase64() As String示例:
vb
Dim base64 As String
base64 = VBMAN.FileEx.OpenFile("C:\\image.png").ReadData.ReturnBase64ReturnHex
返回缓冲区内容为 Hex 字符串
vb
Public Function ReturnHex(Optional ByVal Separator As String = " ") As String示例:
vb
Dim hex As String
hex = VBMAN.FileEx.OpenFile("C:\\data.bin").ReadData.ReturnHex(" ")
' 结果: 48 65 6C 6C 6FReturnBinString
返回缓冲区内容为二进制字符串
vb
Public Function ReturnBinString(Optional ByVal Separator As String = " ") As String示例:
vb
Dim bin As String
bin = VBMAN.FileEx.OpenFile("C:\\data.bin").ReadData.ReturnBinString(" ")
' 结果: 01001000 01100101数据写入
SaveData
将缓冲区写入文件
vb
Public Function SaveData(Optional ByVal FilePath As String, Optional ByVal IsAppend As Boolean = False) As cFileEx参数:
FilePath- 目标路径(默认使用 OpenFile 的路径)IsAppend- 是否追加(默认 False=覆盖)
示例:
vb
' 写入新文件
VBMAN.FileEx.SetBufferText("Hello World", "UTF-8").SaveData "C:\\output.txt"
' 追加到文件
VBMAN.FileEx.SetBufferText("New Line" & vbCrLf, "UTF-8").SaveData "C:\\log.txt", True综合示例
示例1: 快速读取文本文件
vb
Private Function ReadTextFile(filePath As String, Optional charSet As String = "UTF-8") As String
On Error GoTo ErrorHandler
ReadTextFile = VBMAN.FileEx.OpenFile(filePath, "R").ReadData.ReturnText(charSet)
VBMAN.FileEx.CloseFile
Exit Function
ErrorHandler:
ReadTextFile = ""
End Function
Private Sub TestRead()
Dim content As String
content = ReadTextFile("C:\\data.txt", "UTF-8")
TextBox1.Text = content
End Sub示例2: 快速写入文本文件
vb
Private Sub WriteTextFile(filePath As String, content As String, Optional charSet As String = "UTF-8")
On Error GoTo ErrorHandler
VBMAN.FileEx.SetBufferText(content, charSet).SaveData filePath
Exit Sub
ErrorHandler:
MsgBox "写入失败: " & Err.Description
End Sub
Private Sub TestWrite()
WriteTextFile "C:\\output.txt", TextBox1.Text, "UTF-8"
End Sub示例3: 文件编码转换
vb
Private Sub ConvertFileEncoding(sourcePath As String, destPath As String, sourceCharset As String, destCharset As String)
' 读取源文件
Dim content As String
content = VBMAN.FileEx.OpenFile(sourcePath, "R").ReadData.ReturnText(sourceCharset)
VBMAN.FileEx.CloseFile
' 写入目标文件
VBMAN.FileEx.SetBufferText(content, destCharset).SaveData destPath
MsgBox "转换完成!"
End Sub
Private Sub TestConvert()
' GB2312 转 UTF-8
ConvertFileEncoding "C:\\gbk.txt", "C:\\utf8.txt", "GB2312", "UTF-8"
End Sub示例4: 处理二进制文件
vb
Private Sub ProcessBinaryFile()
' 读取二进制文件
VBMAN.FileEx.OpenFile "C:\\data.bin", "R"
' 读取前 16 字节
Dim hexData As String
hexData = VBMAN.FileEx.ReadData(1, 16).ReturnHex(" ")
Debug.Print "Hex: " & hexData
' 读取全部并转 Base64
Dim base64Data As String
base64Data = VBMAN.FileEx.ReadData.ReturnBase64
VBMAN.FileEx.CloseFile
' 保存 Base64 到文本文件
VBMAN.FileEx.SetBufferText(base64Data, "UTF-8").SaveData "C:\\base64.txt"
End Sub示例5: 大文件分块读取
vb
Private Sub ReadLargeFile(filePath As String)
Const CHUNK_SIZE = 1048576 ' 1MB
VBMAN.FileEx.OpenFile filePath, "R"
Dim fileSize As Currency
fileSize = VBMAN.FileEx.FileSize
Dim totalRead As Currency
totalRead = 0
Do While totalRead < fileSize
Dim startPos As Long
Dim endPos As Long
startPos = totalRead + 1
endPos = totalRead + CHUNK_SIZE
If endPos > fileSize Then endPos = fileSize
' 读取一块数据
Dim chunk As String
chunk = VBMAN.FileEx.ReadData(startPos, endPos).ReturnText("UTF-8")
' 处理这块数据...
ProcessChunk chunk
totalRead = endPos
' 显示进度
Debug.Print "进度: " & Int(totalRead / fileSize * 100) & "%"
DoEvents
Loop
VBMAN.FileEx.CloseFile
End Sub示例6: 日志文件追加
vb
Private Sub AppendLog(message As String)
Dim logEntry As String
logEntry = Format(Now, "yyyy-MM-dd hh:mm:ss") & " " & message & vbCrLf
VBMAN.FileEx.SetBufferText(logEntry, "UTF-8").SaveData App.Path & "\\app.log", True
End Sub
Private Sub TestLog()
AppendLog "程序启动"
AppendLog "执行操作 A"
AppendLog "执行操作 B"
End Sub最佳实践
- 及时关闭: 文件使用完毕后及时调用 CloseFile
- 错误处理: 文件操作添加错误处理机制
- 编码一致: 读写使用相同的字符编码
- 大文件分块: 超过内存容量的文件分块读写
- 路径检查: 操作前检查文件/目录是否存在