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. (面试题)有关Integer

    今天在一家公司做了个面试题:运行下列代码,输出结果是什么 Integer a=new Integer("12");  Integer b=new Integer("12& ...

  2. 转:ECharts图表组件入门教程之Theme:ECharts图表的皮肤是什么?如何给图表换主题(皮肤)Theme?

    一.什么是ECharts图表的皮肤(主题)? 针对这个问题我只能这样回答,ECharts图表的主题(皮肤)就犹如人的衣服一样,是用来衬托和渲染主体,使其变得更加美观好看的目的.你去过ECharts图表 ...

  3. Mac使用技巧总结-如何独立设置Mac触摸板方向和鼠标滚轮方向?

    Mac使用技巧总结 如何独立设置Mac触摸板方向和鼠标滚轮方向? 苹果Macbook的使用者都知道,Mac自带的触控板非常好用,不仅支持多手势操控,而且手感极佳,使用流畅. 但是如果对鼠标的焦距有高有 ...

  4. 增加nginx虚拟主机配置文件(conf.d)

    有时候我们按照了nginx后发现配置文件只有一个,/etc/nginx/nginx.conf 所有的配置包括虚拟目录也在此文件中配置, 这样当虚拟主机多了管理就有些不方便了, 这是需要我们把配置文件拆 ...

  5. JS两日期相减

    JS两日期相减,主要用到下面两个方法 dateObject.setFullYear(year,month,day) 方法 stringObject.split(separator) 方法 functi ...

  6. PCIE协议解析 synopsys IP loopback 读书笔记(1)

    1      Overview Core支持单个Pcie内核的Loopback功能,该功能主要为了做芯片验证,以及在没有远程接收器件的情况下完成自己的回环.同时,Core也支持有远程接收器件的loop ...

  7. android: getDimension, getDimensionPixelOffset 和getDimensionPixelSize 区别

    ◆结论: getDimension 获取某个dimen的值,如果是dp或sp的单位,将其乘以density,如果是px,则不乘   返回float getDimensionPixelOffset 获取 ...

  8. Fetch API 接口参考

    前言 Fetch API是新的ajax解决方案,用于解决古老的XHR对象不能实现的问题,Fetch API 提供了一个获取资源的接口(包括跨域请求),任何使用过 XMLHttpRequest 的人都能 ...

  9. angular学习笔记(三十)-指令(6)-transclude()方法(又称linker()方法)-模拟ng-repeat指令

    在angular学习笔记(三十)-指令(4)-transclude文章的末尾提到了,如果在指令中需要反复使用被嵌套的那一坨,需要使用transclude()方法. 在angular学习笔记(三十)-指 ...

  10. git push remote error解决办法

    通常在用git clone了remote端(服务器)的git仓库后,再进行了自己一系列修改后,会将自己测试后稳定的状态push到remote端,以更新源仓库,使 其他人在pull的时候得到自己的修改. ...