针对VB.NET文件流读、写类文件演示实例

本人就非常喜欢对文件进行研究,在网上和书上也收了许多相关的资料,下面的就是一个对VB.NET文件流读、写的类文件。大家可以下载回去研究一下。

VB.NET文件流代码:

 
 
 
  1. Option Explicit  
  2. Private m_strFilePath As String  
  3. Private m_intFileNum As Integer  
  4. Private m_bytBuffer() As Byte  
  5.  
  6. Public Property Get FilePath() As String  
  7. FilePath = m_strFilePath 
  8. End Property  
  9.  
  10. Public Property Let FilePath(ByVal strFilePath As String)  
  11. m_strFilePath = strFilePath  
  12. End Property  
  13.  
  14. Public Property Get EOS() As Boolean  
  15. If Ready() Then  
  16. EOS = EOF(m_intFileNum)  
  17. Else  
  18. EOS = True 
  19. End If  
  20. End Property  
  21.  
  22. Public Property Get Ready() As Boolean  
  23. Ready = m_intFileNum <> 0  
  24. End Property  
  25.  
  26. Public Function CloseFile() As Boolean  
  27. If Ready() Then  
  28. Close #m_intFileNum  
  29. m_intFileNum = 0 
  30. CloseFile = True 
  31. Else  
  32. CloseFile = False 
  33. End If  
  34. End Function  
  35.  
  36. Public Function OpenFile() As Boolean  
  37. On Error Goto HandleError  
  38. CloseFile  
  39. m_intFileNum = FreeFile 
  40. Open m_strFilePath For Binary As #m_intFileNum  
  41. OpenFile = True 
  42. Exit Function  
  43. HandleError:  
  44. OpenFile = False 
  45. End Function  
  46.  
  47. Public Property Get Position() As Long  
  48. If Ready() Then  
  49. Position = Loc(m_intFileNum)  
  50. Else  
  51. Position = -1  
  52. End If  
  53. End Property  
  54.  
  55. Public Property Let Position(ByVal lngPosition As Long)  
  56. If Ready() Then  
  57. If lngPosition > 0 And lngPosition <= LOF(m_intFileNum) Then  
  58. Seek #m_intFileNum, lngPosition  
  59. Else  
  60. RaiseError "Position", "Position invalid"  
  61. End If  
  62. Else  
  63. RaiseError "Position"  
  64. End If  
  65. End Property  
  66.  
  67. Private Sub RaiseError(ByVal strProcedure As String, _  
  68. Optional ByVal strDescription As String = "File Not Opened")  
  69. Err.Raise vbObjectError + 101, strProcedure, strDescription  
  70. End Sub  
  71.  
  72. Public Function ReadBytes(ByVal lngCount As Long) As Byte()  
  73.  
  74. If Ready() Then  
  75. If lngCount > 0 And lngCount + Loc(m_intFileNum) - 1 <= LOF(m_intFileNum) Then  
  76. ReDim m_bytBuffer(0 To lngCount - 1) As Byte  
  77. Get #m_intFileNum, , m_bytBuffer  
  78. ReadBytes = m_bytBuffer 
  79. Else  
  80. RaiseError "ReadBytes", "Out of boundary"  
  81. End If  
  82. Else  
  83. RaiseError "ReadBytes"  
  84. End If  
  85. End Function  
  86.  
  87. Public Function ReadText(ByVal lngCount As Long) As String  
  88. ReadText = StrConv(ReadBytes(lngCount), vbUnicode)  
  89. End Function  
  90.  
  91. Public Sub WriteBytes(ByRef bytContent() As Byte)  
  92. If Ready() Then  
  93. Put #m_intFileNum, , bytContent  
  94. Else  
  95. RaiseError "WriteBytes"  
  96. End If  
  97. End Sub  
  98.  
  99. Public Sub WriteText(ByVal strText As String)  
  100. WriteBytes StrConv(strText, vbFromUnicode)  
  101. End Sub  
  102.  
  103. Private Sub Class_Initialize()  
  104. m_intFileNum = 0 
  105. End Sub  
  106.  
  107. Private Sub Class_Terminate()  
  108. CloseFile  
  109. End Sub 

上述的代码看懂了吗?以后我还会发关于VB.NET文件流相关操作的代码实例,希望大家继续关注。

【编辑推荐】

  1. 实例讲述VB.NET使用Log4Net
  2. 三分钟学会VB.NET转换形态
  3. VB.NET获取硬盘信息四大法宝
  4. 讲述VB.NET调用Excel的好处
  5. 简单例子概述VB.NET新窗体

当前名称:针对VB.NET文件流读、写类文件演示实例
网页地址:http://www.hantingmc.com/qtweb/news6/315806.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联