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

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

文件头:

#include <QFile>
#include <QString>
#include <iostream>
#include <QTextCodec>
#include <QTextStream>

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);
    }
}

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

你可能感兴趣的:(File类)