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. 64位FreeSWITCH编译安装(版本1.4.20)

    1.安装64位的CentOS6.5操作系统(勾选的服务器版本安装). 2.下载FreeSWITCH安装包以及关联的lib库.下载地址http://files.freeswitch.org/downlo ...

  2. 【转载】centos 安装及配置 mysql5.5.3 - rpm安装server和client

    安装 https://blog.csdn.net/cxy1238/article/details/2518480 1. 设置root用户的密码 方法一: # mysqladmin -u root -p ...

  3. VS Code 中文注释显示乱码 解决方法

    将设置中的"files.autoGuessEncoding"项的值改为true即可. 1.文件 2.首选项 3.设置 4.搜索 "files.autoGuessEncod ...

  4. CentOS7.2安装pure-ftpd 及其配置项

    CentOS7.2安装FTP(pure-ftpd-1.0.43) 原文链接: https://www.linuxidc.com/Linux/2016-10/135971.htm [日期:2016-10 ...

  5. Linux 命令 统计进程数目

    ps -efL | grep python | wc -l 此命令的意思是查看 Python的进程数目 ps -ef|grep python|grep -v grep|cut -c -|xargs k ...

  6. ${HADOOP_CONF_DIR:-$HADOOP_PREFIX/$DEFAULT_CONF_DIR}

      在hadoop-config.sh中,有如下语句:${HADOOP_CONF_DIR:-$HADOOP_PREFIX/$DEFAULT_CONF_DIR}                     ...

  7. php分享二十七:批量插入mysql

    一:思考 1:如果插入的某个字段大于数据库定义的长度了,数据库会怎么处理? 1>如果数据库引擎是myisam,则数据库会截断后插入,不报错 2>如果数据库引擎是innodb,则数据库会报 ...

  8. php分享十三:mysql事物

    一:事物的隔离级别 1:隔离级别的类型 SQL标准定义了4类隔离级别,包括了一些具体规则,用来限定事务内外的哪些改变是可见的,哪些是不可见的: Read Uncommitted(读取未提交内容) Re ...

  9. Android 加载大图

    在 Android 开发中, Bitmap 是个吃内存大户,稍微操作不当就会 OOM .虽然现在第三方的图片加载库已经很多,很完善,但是作为一个 Androider 还得知道如何自己进行操作来加载大图 ...

  10. Android开源之BaseRecyclerViewAdapterHelper(持续更新!)

    官方地址:http://www.recyclerview.org/ 文档 v1.9.8 English 中文 v2.0.0 English 中文 Extension library PinnedSec ...