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

vba的一个File操作类的更多相关文章

  1. vba的一个DB操作类

    Option Explicit '-------------------------------------------------------- '[Class Name]: DBHelper '[ ...

  2. Java-编写一个jdbc操作类

    1.通过读取文件配置 package 数据库操作类; /* * Db.java Created on 2007年8月20日, 上午 8:37 */ import java.io.*; import j ...

  3. 一个数据库操作类,适用于Oracle,ACCESS,SQLSERVER

    最近做了一个数据诊断的项目,里面自己写了一个数据库的操作类,包含:连接数据库.读数据表.执行SQL操作,释放数据库等组成,希望对大家有用,由于水平有限,若有错误或者代码不足地方欢迎指正,谢谢. ADO ...

  4. 51)PHP,一个数据库操作类的代码

    <?php //类名,也习惯上(推荐)使用跟文件名相似的名字 //定义一个mysql连接类,该类可以连接mysql数据库 //并实现其单例模式 //该类的功能还能够完成如下基本mysql操作: ...

  5. 一个端口操作类PortHelper

    此操作类主要使用了系统命令行来实现的,详细的命令可参照微软文档<如何使用"netsh advfirewall firewall"上下文而非“netsh firewall”上下 ...

  6. 封装一个redis操作类来操作hash格式

    最近项目要用redis,依然是基于tp3.2. 发现thinkphp3.2自带的缓存类并不好使用,就自己封装了一个 目前只支持hash格式,其他数据类型的操作后面用到的时候再补充 <?php / ...

  7. C#常用操作类库四(File操作类)

    public class FileHelper : IDisposable { private bool _alreadyDispose = false; #region 构造函数 public Fi ...

  8. C#封装的一个JSON操作类

    using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...

  9. Android打造属于自己的数据库操作类。

    1.概述 开发Android的同学都知道sdk已经为我们提供了一个SQLiteOpenHelper类来创建和管理SQLite数据库,通过写一个子类去继承它,就可以方便的创建.管理数据库.但是当我们需要 ...

随机推荐

  1. [trouble shoot]atol和atoll

    就终于的结果来看,事实上就是一个小的错误. 但定位错误的时间比較漫长了.. . 背景:出错的代码是 一段执行在 linux server上的程序,程序的主要功能是处理银行pos刷卡记录并做一些计算.最 ...

  2. springboot 整合 Redis 方法二

    方法一请参考之前博文 spring boot 整合 redis 自己的版本  java8 + redis3.0 + springboot 2.0.0 1 spring boot已经支持集成 redis ...

  3. 如何更新 Visual Studio 2017 的离线安装包

    现在 Visual Studio 2017 已经不再使用原来的 iso 镜像提供离线安装包了,需要的话,可以通过命令行参数下载离线安装包,例如: vs_Enterprise.exe --layout ...

  4. Android 应用架构 - Google 推荐

    Android 应用框架,一般都是使用的 MVC ,MVP ,MVVM 框架,目前 Google 推出了 Android 官方的应用框架. 用到的关键内容: LiveData, ViewModel, ...

  5. Chrome多线程下载

    https://github.com/jae-jae/camtd https://aria2.github.io/ 切换到解压目录D:\Tools\aria2-1.34.0-win-64bit中,打开 ...

  6. 在/etc/my.cnf未配置 [clIent] 导到mysql找不到socket

    [root@DB mysql-5.6.16]# ./bin/mysql ERROR 2002 (HY000): Can't connect to local MySQL server through ...

  7. ex:Could not load file or assembly 'System.Web.Helpers, Version=2.0.0.0, Culture=neutral, . 系统找不到指定的文件。

    今天写的是一个小程序,采用webfrom 形式,.netframework4.0 项目中调用了System.Web.Helpers下的Json方法. 在本地测试没问题,结果搭建到服务器上,死活运行不正 ...

  8. [Windows Azure] Developing Multi-Tenant Web Applications with Windows Azure AD

    Developing Multi-Tenant Web Applications with Windows Azure AD 2 out of 3 rated this helpful - Rate ...

  9. django rest_framework入门二-序列化

    在前一节中,我们已经粗略地介绍了rest_framework的作用之一序列化,下面我们将详细探究序列化的使用. 1.新建一个app snippets python manage.py startapp ...

  10. 李洪强和你一起学习前端之(9)规避脱标,CSS可见性,滑动门案例

    1  复习昨天知识 1.1 浮动  特点: >浮动的元素不占位置(脱标) >可以将行内元素转化为行内块元素 >块级元素在一行上显示 >设置了浮动的元素,影响其后面的元素   作 ...