1. 程式人生 > >Qt筆記——QFile,QDataStream,QTextStream

Qt筆記——QFile,QDataStream,QTextStream

ear read codec qdebug close RR arr ets write

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