欢迎光临:
  
  非常感谢您光临枕善居。本站是一个免费的基于VB,VB.NET源代码交流的平台,为大家提供优质的专业的源代码,如果您有需要,本站可以帮助在业余时间里给您寻找代码。当然,如果您有好的代码也可以在本站发布,共享给大家。
专业VB和.NET源码、编程开发教程、图标资源、USB电脑遥控器、智能家电控制开关....更多东东请进入我的淘宝小店--->
VB及.NET新源码2011(3DVD,控件+资源) 智能多路控制(串口编程开关) 带源码!


05-04
14

文件流的读、写操作的类

这个代码是一个对文件读、写的类文件。大家可以下下载回去研究一下。

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


相关日志:
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
评论: 2 | 引用: 0 | 查看次数: 6790

回复回复ahuatian2008 [2008-01-27 16:26:55 |  | del]
谢谢分享!!
回复回复yxp5401 [2005-04-17 22:39:50 |  | del]
对于文件的写入,我发现以随机方式打开的每条记录只能有60个字节,是不是我没找到方法啊?
发表评论
您没有权限发表评论!