Qt模拟C#的File类对文件进行操作

时间:2023-03-10 01:05:16
Qt模拟C#的File类对文件进行操作

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

文件头:

#include <QFile>
#include <QString>
#include <iostream>
#include <QTextCodec>
#include <QTextStream> enum Encoding
{
ASCII = ,
Unicode = ,
UTF32 = ,
UTF7 = ,
UTF8 = ,
GBK =
}; 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():
c = QTextCodec::codecForName ("ASCII");
break;
case Encoding():
c = QTextCodec::codecForName ("Unicode");
break;
case Encoding():
c = QTextCodec::codecForName ("UTF-32");
break;
case Encoding():
c = QTextCodec::codecForName ("UTF-7");
break;
case Encoding():
c = QTextCodec::codecForName ("UTF-8");
break;
case Encoding():
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 ();
text = stream.readAll ();
}
return text;
} QString File::ReadAllText (QString path)
{
return ReadAllText(path,Encoding());
} QStringList File::ReadAllLines (QString path, Encoding encode)
{
QStringList ts;
if(Exist (path))
{
QFile file(path);
QTextStream stream(&file);
stream.setCodec (GetCode(encode));
stream.seek ();
int index = ;
while(!stream.atEnd ())
{
QString t = stream.readLine (index);
ts.append (t);
index++;
}
}
return ts;
} QStringList File::ReadAllLines (QString path)
{
return ReadAllLines(path,Encoding());
} void File::WriteAllText (QString path, QString content, Encoding encode)
{
Delete(path);
Create(path);
QFile file(path);
QTextStream stream(&file);
stream.seek ();
stream.setCodec (GetCode (encode));
stream << content;
file.close ();
} void File::WriteAllText (QString path, QString content)
{
WriteAllText(path,content,Encoding());
} void File::WriteAllLines (QString path, QStringList content, Encoding encode)
{
Delete(path);
Create(path);
QFile file(path);
QTextStream stream(&file);
stream.seek ();
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());
} 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());
} 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);
}
}

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