19. Qfile file reading and writing operations

1. File reading

We want to click pushButtonthe button and enter the file dialog box to select the file. The upper lineEditbox saves the file path and the lower box textEditsaves the file content.

1.1. Read utf-8 files (default)

method:

  • Open file dialog path——getOpenFileName();
  • Read file——file();
  • File opening method——open();
  • Read file——readAll();
  • Set dialog text——setText();
  • Close the file——close();
 //点击选取文件按钮,弹出文件对话框
    connect(ui->pushButton,&QPushButton::clicked,[=](){
        QString path = QFileDialog::getOpenFileName(this,"打开文件","F:\\Study\\Qt\\Projects\\13_Qt_QFile");
        //将路径放入到lineEdit中
        ui->lineEdit->setText(path);

        //读取内容,放入到 textEdit中
        //QFile默认的支持格式是 utf-8
        QFile file(path);//参数就是读取文件的路径
        //设置打开方式
        file.open(QIODevice::ReadOnly);

        QByteArray array = file.readAll();

        //将读取到的数据 放入textEdit中7
        ui->textEdit->setText(array);

        //对文件对象进行关闭
        file.close();

The effect is as follows:

1.2. Read other types of files (such as gbk)

On the basis of reading utf-8, we add an encoding format class

        //编码格式类
        QTextCodec *codec = QTextCodec::codecForName("gbk");

Then we set the array where the text is written to gbk

        ui->textEdit->setText(codec->toUnicode(array));

The final code is as follows:

    //点击选取文件按钮,弹出文件对话框
    connect(ui->pushButton,&QPushButton::clicked,[=](){
        QString path = QFileDialog::getOpenFileName(this,"打开文件","F:\\Study\\Qt\\Projects\\13_Qt_QFile");
        //将路径放入到lineEdit中
        ui->lineEdit->setText(path);

        //编码格式类
        QTextCodec *codec = QTextCodec::codecForName("gbk");

        //读取内容,放入到 textEdit中
        //QFile默认的支持格式是 utf-8
        QFile file(path);//参数就是读取文件的路径
        //设置打开方式
        file.open(QIODevice::ReadOnly);

        QByteArray array;
        while(!file.atEnd()){
             array += file.readLine();//按行读
        }

        //将读取到的数据 放入textEdit中7
        ui->textEdit->setText(codec->toUnicode(array));

        //对文件对象进行关闭
        file.close();

The effect is as follows:

You can see that we successfully opened the file in gbk format


2. File writing

We simply add some text to the end of the file

        //进行写文件
        file.open(QIODevice::Append);//用追加的方式写
        file.write("啊啊啊啊啊");
        file.close();

The effect is as follows:

You can see that the addition was successful (this is because we ran it multiple times)


3. Display file information

In addition to getting the content of the file, we also want to view the specific information of the file, so we can QFileInfodo this through the file information class.

  • File size--size();
  • File extension--suffix();
  • file name--fileName();
  • file path--filePath();
  • File creation date -birthTime();
  • Date the file was last modified -lastModified();
        //QFileInfo文件信息类
        QFileInfo info(path);

        qDebug()<<"大小"<<info.size()<<"后缀名"<<info.suffix()<<"文件名称"<<info.fileName()<<"文件路径"<<info.filePath();
        qDebug()<<"创建日期"<<info.birthTime().toString("yyyy/MM/dd hh:mm:ss");
        qDebug()<<"最后修改日期"<<info.lastModified().toString("yyyy-MM-dd hh:mm:ss");

The effect is as follows:

Replenish:

  • For more functions about file information, please refer to Qt Assistant

  • The date must be converted to obtain a pure date display, otherwise there will be other additional information. The specific display format of the date can be set by yourself yyyy-MM-dd hh:mm:ss. Please refer to Qt Assistant

4. Reference to relevant program files

13_Qt_QFile.zip

Guess you like

Origin blog.csdn.net/qq_63388834/article/details/135166304