Qt simulates C # file operations on files

Source: Internet
Author: User

Actually, it's just that QT cainiao made it for practice.

File Header:

# 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); // create a file, if the object exists, it overwrites static bool exist (qstring path); // determines whether the object exists static void appendalltext (qstring path, qstring content, encoding encode ); // append the static void appendalltext (qstring path, qstring content) to the existing file; // append the static void appendalllines (qstring path, qstringlist lines, encoding encode); // append multiple rows of data to an existing file. Static void appendalllines (qstring path, qstringlist lines ); // append multiple rows of data to an existing file static void writealltext (qstring path, qstring content, encoding encode); // create a new file and write it into the file, if a file exists, it will overwrite static void writealltext (qstring path, qstring content); // create a new file and write it into the file. If a file exists, it will overwrite static void writealllines (qstring path, qstringlist content, encoding encode); // create a new file and write multiple lines of data. If the file exists, it overwrites static void writealllines (qstring path, qstringlist content ); // create a new file and write data in multiple rows. If the file exists, it overwrites static qstring readalltext (qstring path, encoding encode); // read the static qstring readalltext (qstring path) file ); // read the file static qstringlist readalllines (qstring path, encoding encode); // read all rows in the file static qstringlist readalllines (qstring path ); // read all rows in the file static void copy (qstring sourcefilename, qstring destfilename, bool overwrite); // copy the file static void copy (qstring sourcefilename, qstring destfilename ); // copy the file static void move (qstring sourcefilename, qstring destfilename); // move the file static void Delete (qstring path); // delete the file static void Rename (qstring path, qstring name); // Rename private: static qtextcodec * getcode (encoding Code );};

Source file:

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

It took a lot of time to get it done, but I didn't do the test. I don't know if there are any problems ......

Qt simulates C # file operations on files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.