Qt之QFile的介绍和使用

发布时间 2023-05-17 16:01:21作者: 青衣守旧人

  QFile是Qt框架中的一个类,用于对文件进行读取和写入操作。它提供了一种方便的方式来处理文件的操作,包括创建、打开、读取、写入、修改和关闭文件。以下是QFile类的一些常见用途:

  1. 文件读取:使用QFile可以打开文件并从中读取数据。您可以使用QFile的read()或readAll()函数来读取文件内容,并根据需要进行解析或处理。

  2. 文件写入:使用QFile可以创建或打开文件,并将数据写入其中。您可以使用QFile的write()函数将数据写入文件,例如保存应用程序的配置或输出结果到文件中。

  3. 文件拷贝和移动:QFile提供了拷贝和移动文件的功能。您可以使用QFile的copy()函数将文件从一个位置复制到另一个位置,或使用rename()函数将文件从一个位置移动到另一个位置。

  4. 文件属性和信息:QFile类还提供了访问文件属性和信息的功能。您可以使用QFile的size()函数获取文件的大小,permissions()函数获取文件的权限,以及其他函数获取文件的创建时间、修改时间等信息。

  5. 文件检查和存在性判断:QFile允许您检查文件是否存在,并根据需要执行相应的操作。您可以使用QFile的exists()函数检查文件是否存在,并根据返回值来处理文件的存在与否。

总之,QFile是Qt中用于文件操作的基本类,提供了读取、写入、拷贝、移动和访问文件属性等功能。它使得文件的处理变得简单和方便,并与Qt的其他模块和类无缝集成,使得文件操作与应用程序的其他部分相互配合更加容易。

一、直接使用QFile读写文件

#include <QCoreApplication>

#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString file_name = QCoreApplication::applicationDirPath() + "/test.txt";
    QFile file(file_name);
    bool ret = file.open(QIODevice::WriteOnly | QIODevice::Text);
    if (ret) {
        QString data = "Hello, World!\n";
        QByteArray byteArray = data.toUtf8();
        file.write(byteArray);

        char buffer[256] = {"Helllo Qt!"};
        file.write(buffer, strlen(buffer));

        file.close();

    }

    return a.exec();
}
#include <QCoreApplication>

#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    /* 读取文件信息 */
    QString file_name = QCoreApplication::applicationDirPath() + "/test.txt";
    QFile file(file_name);
    if (!file.exists()) {
        qDebug() << "文件不存在";
        return -1;
    }
    bool ret = file.open(QIODevice::ReadOnly | QIODevice::Text);
    if (ret) {
        /* 读取每一行的数据 */
        while (!file.atEnd()) {
            QByteArray info = file.readLine();
            qDebug() << info;
        }

        /* 读取所有的数据 */
        //qDebug() << file.readAll();
        //qDebug() << file.read(1024);

        /* 返回文件信息的实际字节大小 */
        //char buffer[1024] = {0};
        //qDebug() << file.read(buffer, sizeof(buffer));

        file.close();
    }

    return a.exec();
}

二、使用QDataStream读写文件

#include <QCoreApplication>
#include <QFile>
#include <QDataStream>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile file("example.dat");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qDebug() << "文件打开错误";
        return -1;
    }

    QDataStream out(&file);
    out << QString("Hello World!");
    out << 42;
    out << 3.14;

    file.close();

    return a.exec();
}
#include <QCoreApplication>
#include <QFile>
#include <QDataStream>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile file("example.dat");
    if (!file.exists()) {
        qDebug() << "文件不存在";
        
        return -1;
    }
    
    if (!file.open(QIODevice::ReadOnly)) {
        qDebug() << "文件打开失败";
        
        return  -1;
    }
    
    QDataStream in(&file);
    QString str;
    int value;
    double pi;
    
    in >> str >> value >> pi;
    
    file.close();
    
    qDebug() << str << "," << value << "," << pi;

    return a.exec();
}

三、使用QTextStream读写文件

#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile file("example.txt");
    if (!file.open(QIODevice::ReadWrite | QIODevice::WriteOnly | QIODevice::Text)) {
        qDebug() << "文件打开失败";

        return -1;
    }

    QTextStream stream(&file);

    /* 写入文件 */
    stream << "Hello World!" << Qt::endl;
    stream << 43 << Qt::endl;
    stream << 3.14  << Qt::endl;

    /* 将文件指针移动到文件开头 */
    file.seek(0);

    /* 读取文件 */
    QString line;
    while (!stream.atEnd()) {
        line = stream.readLine();
        qDebug() << line;
    }

    file.close();

    return a.exec();
}

QDataStream和QTextStream是Qt框架中用于读写数据的两个重要类,它们之间有一些区别和适用场景的差异。

1. 数据类型支持:
QDataStream`支持对多种数据类型进行序列化和反序列化,包括基本类型(例如整数、浮点数)、自定义类对象、Qt类对象等。`QDataStream`以二进制格式读写数据,适用于需要直接访问二进制数据的场景。
QTextStream`主要用于对文本数据的读写操作。它提供了方便的文本处理方法,可以处理字符编码、换行符等特殊情况,并提供了逐行读取和写入文本的能力。但是,`QTextStream`不支持直接序列化和反序列化自定义类对象。

2. 数据格式:
QDataStream以二进制格式读写数据,不会对数据进行格式化处理,保留原始的二进制表示。因此,它适用于需要保持数据准确性和精确度的场景。
QTextStream以文本格式读写数据,会对数据进行字符编码处理,并且可以按照特定格式进行文本的读写和格式化输出。

3. 可读性和可编辑性:
QDataStream生成的数据通常是二进制的,不易于直接阅读和编辑。因为它主要用于数据的传输和持久化存储,而不是人类可读的格式。
QTextStream生成的数据是以文本形式存储的,易于阅读和编辑。它适用于需要人类可读性和可编辑性的文本数据存储场景。

4. 平台和语言无关性:
QDataStream`的数据格式是平台和语言无关的,因此可以在不同平台和使用不同编程语言的应用程序之间进行数据交换。
QTextStream主要用于文本数据的读写,而文本数据的编码和格式在不同平台和语言之间可能存在差异,因此需要注意字符编码和换行符等细节。

综上所述,`QDataStream`适合处理二进制数据、自定义类对象的序列化和反序列化,而`QTextStream`适合处理文本数据的读写和文本格式化输出。根据具体的需求和数据类型,选择适合的类来进行读写操作。

四、使用QFile::rename修改文件名称

#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile file("example.txt");
    if (!file.exists()) {
        qDebug() << "文件不存在";
        return -1;
    }

    //file.rename("example.txt");
    file.rename("example.txt", "example_01.txt");

    return a.exec();
}

五、使用QFile::copy拷贝文件

#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile file("example.txt");
    if (!file.exists()) {
        qDebug() << "文件不存在";
        return -1;
    }

    file.copy("example.txt", "example_01.txt");

    return a.exec();
}

六、使用QFile::remove删除文件

#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile file("example.txt");
    if (!file.exists()) {
        qDebug() << "文件不存在";
        return -1;
    }

    file.remove();

    return a.exec();
}