博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt模拟C#的File类对文件进行操作
阅读量:4608 次
发布时间:2019-06-09

本文共 6113 字,大约阅读时间需要 20 分钟。

其实只是QT菜鸟为了练习而搞出来的

文件头:

#include 
#include
#include
#include
#include
enum Encoding{ ASCII = 0, Unicode = 1, UTF32 = 2, UTF7 = 3, UTF8 = 4, GBK = 5};class File{public: File(); static void Create(QString path); //创建文件,如果文件存在则覆盖 static bool Exist(QString path); //判断文件是否存在 static void AppendAllText(QString path, QString content, Encoding encode); //向现有文件内追加数据 static void AppendAllText(QString path, QString content); //向现有文件内追加数据 static void AppendAllLines(QString path,QStringList lines, Encoding encode); //向现有文件内追加多行数据 static void AppendAllLines(QString path,QStringList lines); //向现有文件内追加多行数据 static void WriteAllText(QString path,QString content,Encoding encode); //创建新文件并写入文件,如果有文件则覆盖 static void WriteAllText(QString path,QString content); //创建新文件并写入文件,如果有文件则覆盖 static void WriteAllLines(QString path,QStringList content,Encoding encode); //创建新文件并写入多行数据,如果文件存在则覆盖 static void WriteAllLines(QString path,QStringList content); //创建新文件并写入多行数据,如果文件存在则覆盖 static QString ReadAllText(QString path,Encoding encode); //读取文件 static QString ReadAllText(QString path); //读取文件 static QStringList ReadAllLines(QString path,Encoding encode); //读取文件内所有的行 static QStringList ReadAllLines(QString path); //读取文件内所有的行 static void Copy(QString sourceFileName,QString destFileName,bool overwrite); //拷贝文件 static void Copy(QString sourceFileName,QString destFileName); //拷贝文件 static void Move(QString sourceFileName,QString destFileName); //移动文件 static void Delete(QString path); //删除文件 static void Rename(QString path,QString name); //重命名private: static QTextCodec* GetCode(Encoding code);};

源文件:

File::File(){}bool File::Exist (QString path){    QFile file(path);    return file.exists ();}QTextCodec* File::GetCode (Encoding code){    QTextCodec* c;    switch(code)    {    case Encoding(0):        c = QTextCodec::codecForName ("ASCII");        break;    case Encoding(1):        c = QTextCodec::codecForName ("Unicode");        break;    case Encoding(2):        c = QTextCodec::codecForName ("UTF-32");        break;    case Encoding(3):        c = QTextCodec::codecForName ("UTF-7");        break;    case Encoding(4):        c = QTextCodec::codecForName ("UTF-8");        break;    case Encoding(5):        c = QTextCodec::codecForName ("GBK");        break;    }    return c;}void File::Create (QString path){    Delete(path);    QFile file(path);    file.open (QIODevice::WriteOnly);    file.close ();}QString File::ReadAllText (QString path, Encoding encode){    QString text;    if(Exist (path))    {        QFile file(path);        QTextStream stream(&file);        stream.setCodec (GetCode(encode));        stream.seek (0);        text = stream.readAll ();    }    return text;}QString File::ReadAllText (QString path){    return ReadAllText(path,Encoding(1));}QStringList File::ReadAllLines (QString path, Encoding encode){    QStringList ts;    if(Exist (path))    {        QFile file(path);        QTextStream stream(&file);        stream.setCodec (GetCode(encode));        stream.seek (0);        int index = 0;        while(!stream.atEnd ())        {            QString t = stream.readLine (index);            ts.append (t);            index++;        }    }    return ts;}QStringList File::ReadAllLines (QString path){    return ReadAllLines(path,Encoding(1));}void File::WriteAllText (QString path, QString content, Encoding encode){    Delete(path);    Create(path);    QFile file(path);    QTextStream stream(&file);    stream.seek (0);    stream.setCodec (GetCode (encode));    stream << content;    file.close ();}void File::WriteAllText (QString path, QString content){    WriteAllText(path,content,Encoding(1));}void File::WriteAllLines (QString path, QStringList content, Encoding encode){    Delete(path);    Create(path);    QFile file(path);    QTextStream stream(&file);    stream.seek (0);    stream.setCodec (GetCode (encode));    for ( QStringList::Iterator it = content.begin(); it != content.end(); ++it )                stream << *it << "\r\n";    file.close ();}void File::WriteAllLines (QString path, QStringList content){    WriteAllLines(path,content,Encoding(1));}void File::AppendAllText (QString path, QString content, Encoding encode){    if(!Exist (path))Create(path);    QString text = ReadAllText(path, encode);    text += content;    WriteAllText(path,text,encode);}void File::AppendAllText (QString path, QString content){    AppendAllText(path,content,Encoding(1));}void File::AppendAllLines (QString path, QStringList lines, Encoding encode){    if(!Exist (path))Create(path);    QString text = ReadAllText(path, encode);    text += "\r\n";    foreach(QString s,lines)    text += (s + "\r\n");    WriteAllText(path,text,encode);}void File::Copy (QString sourceFileName, QString destFileName, bool overwrite){    if(Exist (destFileName) && overwrite)    {        QFile file(destFileName);        file.remove ();    }    if(!Exist (destFileName))    {        QFile::copy (sourceFileName,destFileName);    }}void File::Copy (QString sourceFileName, QString destFileName){    Copy(sourceFileName,destFileName,false);}void File::Delete (QString path){    QFile file(path);    if(file.exists ())    {        file.remove ();    }}void File::Rename (QString path, QString name){    if(Exist(path))    {        QFile file(path);        QString oldName = file.fileName ();        QFile::rename (oldName,name);    }}

花了不少时间搞定了,不过并没有做测试,有没有问题我也不知道……

转载于:https://www.cnblogs.com/rogation/p/3929964.html

你可能感兴趣的文章
BMP图像格式
查看>>
python的匿名函数lambda解释及用法
查看>>
c#遍历Dictionary使用KeyValuePair
查看>>
defineProperties属性的运用==数据绑定
查看>>
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>
快速入门
查看>>
Mybatis通过ID查询 && 通过name模糊查询
查看>>
发送邮件
查看>>
VirtulBox虚拟机搭建Linux Centos系统
查看>>
创建型模式(二):AbstractFactory ( 抽象工厂 )
查看>>
PHP获取当日或本月时间戳范围
查看>>
一款由jQuery实现的手风琴式相册图片展开效果
查看>>
基于jQuery仿淘宝产品图片放大镜代码
查看>>
Python中Selenium的使用方法
查看>>
python ord()与chr()用法以及区别
查看>>
三月23日测试Fiddler
查看>>
20171013_数据库新环境后期操作
查看>>