Qt笔记——QFile,QDataStream,QTextStream

Posted 大蓝鲸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt笔记——QFile,QDataStream,QTextStream相关的知识,希望对你有一定的参考价值。

QFile

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void on_readButton_clicked();

    void on_writeButton_clicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QFileInfo>
#include <QDateTime>
#include <QDebug>
#include <QDataStream>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_readButton_clicked()
{
    QString path = QFileDialog::getOpenFileName(this,
                                                "open",
                                                "../",
                                                "TXT(*.txt)");
    if(path.isEmpty() == false)
    {
        QFile file(path);
        bool isOK = file.open(QIODevice::ReadOnly);
        if(isOK == true)
        {
#if 0
            //默认只识别utf8编码
            QByteArray array = file.readAll();
            //ui->textEdit->setText(QString(array));
            ui->textEdit->setText(array);
 #endif
            QByteArray array;
            while (file.atEnd() == false)
            {
                array += file.readLine();
            }
            ui->textEdit->setText(array);

        }
        file.close();

        QFileInfo info(path);
        qDebug()<<info.fileName();
        qDebug()<<info.suffix();
        qDebug()<<info.size();
        qDebug()<<info.created().toString("yyyy-MM-dd hh:mm:ss");
    }
}

void Widget::on_writeButton_clicked()
{
    QString path = QFileDialog::getSaveFileName(this,"save","../","TXT(*.txt)");
    if(path.isEmpty() == false)
    {
        QFile file;
        file.setFileName(path);
        bool isOk = file.open(QIODevice::WriteOnly);
        if(isOk == true)
        {
            QString str = ui->textEdit->toPlainText();
            file.write(str.toUtf8());
            //file.write((str.toStdString().data()));
        }
        file.close();
    }
}

 

  

QDataStream

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    void writeData();
    void readData();
private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QDataStream>
#include <QTextStream>
#include <QFile>
#include <QDebug>

#define cout qDebug()<<"["<<__FILE__<<":"<<__LINE__<<"]"
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    writeData();
    readData();
}

Widget::~Widget()
{
    delete ui;
}
void Widget::writeData()
{
    QFile file("../fff.txt");
    bool isOK = file.open(QIODevice::WriteOnly);
    if (true == isOK)
    {
        //创建数据流,和file文件关联
        //往数据流中写数据,相当于往文件里写数据

        QDataStream stream(&file);
        stream<<QString("hello world")<<250;

    }
    file.close();
}

void Widget::readData()
{
    QFile file("../fff.txt");
    bool isOK = file.open(QIODevice::ReadOnly);
    if (true == isOK)
    {
        //创建数据流,和file文件关联
        //往数据流中读数据,相当于往文件里写数据
        QString str;
        int a;
        QDataStream stream(&file);
        stream>>str>>a;
        cout<<str<<a;

    }
    file.close();
}

QTextStream

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    void writeData();
    void readData();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QFileDialog>
#define cout qDebug()<<"["<<__FILE__<<":"<<__LINE__<<"]"
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    writeData();
    readData();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::writeData()
{
    QFile file;
    file.setFileName("../demo.txt");

    bool isOK = file.open(QIODevice::WriteOnly);
    if(true == isOK)
    {
        QTextStream stream(&file);
        //指定编码
        stream.setCodec("UTF-8");
        stream << QString("hello world")<<250;
        file.close();
    }
}

void Widget::readData()
{
    //这么读有问题
    QFile file;
    file.setFileName("../demo.txt");

    bool isOK = file.open(QIODevice::ReadOnly);
    if(true == isOK)
    {
        QTextStream stream(&file);
        //指定编码
        stream.setCodec("UTF-8");
        QString str;
        int a;
        stream >>str>>a;
        cout<<str<<a;
        file.close();
    }
}

void Widget::on_pushButton_clicked()
{
    QString path = QFileDialog::getOpenFileName(this,
                                                "read",
                                                "../"
                                                );
    if(false == path.isEmpty())
    {
        QFile file;
        file.setFileName(path);
        bool isOK = file.open(QIODevice::ReadOnly);
        if(true == isOK)
        {
            QTextStream stream(&file);
            stream.setCodec("UTF-8");
            QString str = stream.readAll();
            ui->textEdit->setText(str);
            file.close();
        }
    }

}

QBuffer

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QBuffer>//内存文件
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QBuffer memFile;
    memFile.open(QIODevice::WriteOnly);

    memFile.write("hello");
    memFile.write("world");

    memFile.close();

    qDebug()<<memFile.buffer();

    QBuffer memFile1;
    memFile1.open(QIODevice::WriteOnly);
    QDataStream stream(&memFile1);
    stream<<QString("测试")<<250;
    memFile1.close();

    qDebug()<<QString(memFile.buffer()).toUtf8().data();//输出有问题
//输出正确
    memFile1.open(QIODevice::ReadOnly);
    QDataStream in(&memFile1);
    QString str;
    int a;
    in>>str>>a;
    memFile1.close();
    qDebug()<<str.toUtf8().data()<<a;
}

Widget::~Widget()
{
    delete ui;
}

 

 

以上是关于Qt笔记——QFile,QDataStream,QTextStream的主要内容,如果未能解决你的问题,请参考以下文章

6.3.1 读写文本示例

Qt QFile文件读写

Qt - QFile类(翻译)

Qt入门教程文件操作篇QFile文件处理对象

QFile/QDataStream 写入现有数据

文件操作QFile和QDataStream