本人就非常喜欢对文件进行研究,在网上和书上也收了许多相关的资料,下面的就是一个对VB.NET文件流读、写的类文件。大家可以下载回去研究一下。
VB.NET文件流代码:
- Option Explicit
- Private m_strFilePath As String
- Private m_intFileNum As Integer
- Private m_bytBuffer() As Byte
- Public Property Get FilePath() As String
- FilePath = m_strFilePath
- End Property
- Public Property Let FilePath(ByVal strFilePath As String)
- m_strFilePath = strFilePath
- End Property
- Public Property Get EOS() As Boolean
- If Ready() Then
- EOS = EOF(m_intFileNum)
- Else
- EOS = True
- End If
- End Property
- Public Property Get Ready() As Boolean
- Ready = m_intFileNum <> 0
- End Property
- Public Function CloseFile() As Boolean
- If Ready() Then
- Close #m_intFileNum
- m_intFileNum = 0
- CloseFile = True
- Else
- CloseFile = False
- End If
- End Function
- Public Function OpenFile() As Boolean
- On Error Goto HandleError
- CloseFile
- m_intFileNum = FreeFile
- Open m_strFilePath For Binary As #m_intFileNum
- OpenFile = True
- Exit Function
- HandleError:
- OpenFile = False
- End Function
- Public Property Get Position() As Long
- If Ready() Then
- Position = Loc(m_intFileNum)
- Else
- Position = -1
- End If
- End Property
- Public Property Let Position(ByVal lngPosition As Long)
- If Ready() Then
- If lngPosition > 0 And lngPosition <= LOF(m_intFileNum) Then
- Seek #m_intFileNum, lngPosition
- Else
- RaiseError "Position", "Position invalid"
- End If
- Else
- RaiseError "Position"
- End If
- End Property
- Private Sub RaiseError(ByVal strProcedure As String, _
- Optional ByVal strDescription As String = "File Not Opened")
- Err.Raise vbObjectError + 101, strProcedure, strDescription
- End Sub
- Public Function ReadBytes(ByVal lngCount As Long) As Byte()
- If Ready() Then
- If lngCount > 0 And lngCount + Loc(m_intFileNum) - 1 <= LOF(m_intFileNum) Then
- ReDim m_bytBuffer(0 To lngCount - 1) As Byte
- Get #m_intFileNum, , m_bytBuffer
- ReadBytes = m_bytBuffer
- Else
- RaiseError "ReadBytes", "Out of boundary"
- End If
- Else
- RaiseError "ReadBytes"
- End If
- End Function
- Public Function ReadText(ByVal lngCount As Long) As String
- ReadText = StrConv(ReadBytes(lngCount), vbUnicode)
- End Function
- Public Sub WriteBytes(ByRef bytContent() As Byte)
- If Ready() Then
- Put #m_intFileNum, , bytContent
- Else
- RaiseError "WriteBytes"
- End If
- End Sub
- Public Sub WriteText(ByVal strText As String)
- WriteBytes StrConv(strText, vbFromUnicode)
- End Sub
- Private Sub Class_Initialize()
- m_intFileNum = 0
- End Sub
- Private Sub Class_Terminate()
- CloseFile
- End Sub
上述的代码看懂了吗?以后我还会发关于VB.NET文件流相关操作的代码实例,希望大家继续关注。
【编辑推荐】
当前名称:针对VB.NET文件流读、写类文件演示实例
网页地址:http://www.shufengxianlan.com/qtweb/news6/315806.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联