您好,登錄后才能下訂單哦!
這篇文章主要介紹如何實(shí)現(xiàn)VB.NET文件流讀、寫(xiě)類文件,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
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
以上是“如何實(shí)現(xiàn)VB.NET文件流讀、寫(xiě)類文件”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。