Option Explicit
'--------------------------------------------------------
'[Class name]: clsTxtFile
'[Description]: Read Or Write Txt File
'-------------------------------------------------------- Private mFileNumber As Integer
Private mIsOpen As Boolean
Private mEncoding As String
Private mStream As Object
Private mFilePath As String '--------------------------------------------------------
'[Function name]: OpenFile
'[Description]: Open file
'[Parameter]: (1) file path (2)encoding (eg:utf-8)
'--------------------------------------------------------
Public Sub OpenFile(path As String, encoding As String) mEncoding = encoding
mFilePath = path
If mEncoding <> "" Then
Set mStream = CreateObject("Adodb.Stream")
With mStream
.Type = '1:binary 2:text
.Mode = '1:Read 2:Write 3:ReadWrite
.Open
.LoadFromFile path
.Charset = encoding
.Position = 'encoding's position
End With
Else
mFileNumber = FreeFile
Open path For Input As #mFileNumber
End If
mIsOpen = True
End Sub '--------------------------------------------------------
'[Function name]: CreateFile
'[Description]: Create file
'[Parameter]: (1) file path (2)encoding
'--------------------------------------------------------
Public Sub CreateFile(path As String, encoding As String) mEncoding = encoding
mFilePath = path CreateFileCore (path) If mEncoding <> "" Then
Set mStream = CreateObject("Adodb.Stream")
With mStream
.Type = '1:binary 2:text
.Mode = '1:Read 2:Write 3:ReadWrite
.Open
.Charset = encoding
End With
Else
mFileNumber = FreeFile
Open path For Binary Access Write As #mFileNumber
End If
mIsOpen = True
End Sub '--------------------------------------------------------
'[Function name]: CreateFileCore
'[Description]: cretae file
'[Parameter]: (1) file path
'--------------------------------------------------------
Private Sub CreateFileCore(path As String) Dim fso As Object
Dim folderName As String Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(path) Then
'file exists,delete
fso.DeleteFile path, True
Else
'file not exists,create
folderName = fso.GetParentFolderName(path)
If Not fso.FolderExists(folderName) Then
fso.CreateFolder (folderName)
End If
End If fso.CreateTextFile path, True
End Sub '--------------------------------------------------------
'[Function name]: ReadLine
'[Description]: read a line
'[Return Value]: line string
'--------------------------------------------------------
Public Function ReadLine() As String Dim strLine As String
If mEncoding <> "" Then
strLine = mStream.ReadText(-) '-1:adReadAll -2:adReadLine
Else
Line Input #mFileNumber, strLine
End If ReadLine = strLine
End Function '--------------------------------------------------------
'[Function name]: WriteLine
'[Description]: Write line
'[Parameter]: (1) line
'--------------------------------------------------------
Public Sub WriteLine(strLine As String) If mEncoding <> "" Then
Call mStream.WriteText(strLine, ) '0:adWriteChar 1:adWriteLine
Else
strLine = strLine & vbCrLf
Put #mFileNumber, , strLine
End If
End Sub '--------------------------------------------------------
'[Function name]: IsEndOfFile
'[Description]: if is the end of the file
'[Return Value]: true:end of the file false:not end of the file
'--------------------------------------------------------
Public Function IsEndOfFile() As Boolean If mEncoding <> "" Then
IsEndOfFile = mStream.EOS
Else
IsEndOfFile = EOF(mFileNumber)
End If
End Function '--------------------------------------------------------
'[Function name]: CloseFile
'[Description]: close file
'--------------------------------------------------------
Public Sub CloseFile() If mIsOpen Then
If mEncoding <> "" Then
mStream.SaveToFile mFilePath, 'adSaveCreateNotExist =1 adSaveCreateOverWrite = 2
mStream.Close
Set mStream = Nothing
Else
Close mFileNumber
End If
End If
End Sub

最新文章

  1. 2013 duilib入门简明教程 -- 部分bug (11)
  2. 【转】Eclipse下启动tomcat报错:/bin/bootstrap.jar which is referenced by the classpath, does not exist.
  3. Entity Framework与ADO.Net及NHibernate的比较
  4. ios开发函数(数学函数应用)
  5. PowerDesigner-制作Word导出模版
  6. chmod修改文件的权限/chown修改文件和目录的所有者
  7. “后PC”时代来临
  8. UIStepper swift
  9. linux 操作
  10. winform开线程,避免页面假死
  11. app界面设计字体规范
  12. 遇到安装app不识别的情况
  13. kubernetes 单节点和多节点环境搭建
  14. android viewpager切换到最后一页时,跳转至其他activity
  15. spyder中让生成的图像单独在窗口中显示
  16. Mysql双主互备+keeplived高可用架构介绍
  17. 【原创】大叔经验分享(38)beeline连接hiveserver2报错impersonate
  18. virtual关键字
  19. JSON与对象的相互转换
  20. Spring Java-based容器配置

热门文章

  1. 如何通过sql的insert语句插入大量字符串到oracle的clob字段?
  2. [转]Greenplum的工作负载及资源管理
  3. JavaScript语言精粹--replace()与正则
  4. sql综合查询with row_number() over 递归和开窗查询
  5. glide 镜像
  6. django缓存基于类的视图
  7. 使用 NodeJS + Express 從 GET/POST Request 取值 -摘自网络
  8. zabbix 对/etc/ssh/sshd_config文件的监控 但status为unknowen
  9. vue-cli+webpack在生成的项目中使用bootstrap方法(一)
  10. 【工具】Sublime + MarkdownEditing + OmniMarkupPreviewer使用起来