新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
針對(duì)VB.NET文件流讀、寫類文件演示實(shí)例
本人就非常喜歡對(duì)文件進(jìn)行研究,在網(wǎng)上和書上也收了許多相關(guān)的資料,下面的就是一個(gè)對(duì)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
上述的代碼看懂了嗎?以后我還會(huì)發(fā)關(guān)于VB.NET文件流相關(guān)操作的代碼實(shí)例,希望大家繼續(xù)關(guān)注。
【編輯推薦】
- 實(shí)例講述VB.NET使用Log4Net
- 三分鐘學(xué)會(huì)VB.NET轉(zhuǎn)換形態(tài)
- VB.NET獲取硬盤信息四大法寶
- 講述VB.NET調(diào)用Excel的好處
- 簡(jiǎn)單例子概述VB.NET新窗體
網(wǎng)頁(yè)題目:針對(duì)VB.NET文件流讀、寫類文件演示實(shí)例
網(wǎng)址分享:http://m.fisionsoft.com.cn/article/dhchjsc.html


咨詢
建站咨詢
