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

开发 后端
文章主要演示了一个关于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新窗体
责任编辑:田树 来源: 博客
相关推荐

2009-11-02 09:45:23

VB.NET文件系统对

2009-10-27 10:58:00

VB.NET文件名排序

2009-11-03 16:43:54

VB.NET拖放文件

2009-10-23 14:31:05

VB.NET类定义

2009-10-22 09:20:46

VB.NET Proc

2009-11-02 12:35:10

VB.NET追加文件

2009-10-29 15:16:02

VB.NET文件传送

2010-01-15 10:05:35

VB.NET文件对象

2010-01-21 16:17:32

VB.NET文件对象

2010-01-20 13:42:10

VB.NET访问INIGetPrivateP

2009-10-29 15:28:38

VB.NET文件操作

2009-10-28 13:24:25

VB.NET文件

2009-11-02 09:21:04

VB.NET文件系统

2009-10-29 15:02:04

VB.NET文件排序

2010-01-15 19:04:09

2009-10-26 09:50:20

VB.NET Star

2009-11-03 11:06:40

VB.NET事件

2009-10-29 13:46:14

VB.NET DES加

2009-10-20 09:39:04

VB.NET Butt

2010-01-18 16:33:57

VB.NET加密文件
点赞
收藏

51CTO技术栈公众号