• QT学习笔记(11) 读写文件


    一、QT的文件系统的类之间的关系

    二、文件的读写

      (1)通过QFile读写文件

      (2)QFileInfo获取文件信息

    代码如下:

    mainwindow.h

     1 #ifndef MAINWINDOW_H
     2 #define MAINWINDOW_H
     3 
     4 #include <QMainWindow>
     5 
     6 namespace Ui {
     7 class MainWindow;
     8 }
     9 
    10 class MainWindow : public QMainWindow
    11 {
    12     Q_OBJECT
    13 
    14 public:
    15     explicit MainWindow(QWidget *parent = 0);
    16     ~MainWindow();
    17 
    18 private slots:
    19 
    20     void on_buttonRead_clicked();
    21 
    22     void on_buttonWrite_clicked();
    23 
    24 private:
    25     Ui::MainWindow *ui;
    26 };
    27 
    28 #endif // MAINWINDOW_H

    mainwindow.cpp

      1 #include "mainwindow.h"
      2 #include "ui_mainwindow.h"
      3 #include <QFile>
      4 #include <QFileDialog>
      5 #include <QDateTime>
      6 #include <QDebug>
      7 
      8 MainWindow::MainWindow(QWidget *parent) :
      9     QMainWindow(parent),
     10     ui(new Ui::MainWindow)
     11 {
     12     ui->setupUi(this);
     13 }
     14 
     15 MainWindow::~MainWindow()
     16 {
     17     delete ui;
     18 }
     19 
     20 void MainWindow::on_buttonRead_clicked()
     21 {
     22     QString path = QFileDialog::getOpenFileName(this,
     23                    "open","../","TXT(*.txt)");
     24     if(path.isEmpty() == false)//不为空
     25     {
     26         QFile file(path);//文件对象
     27         //打开文件,只读方式
     28         bool isOK = file.open(QIODevice::ReadOnly);
     29         if(isOK == true)
     30         {
     31 
     32             /*
     33             //读文件,默认只识别UTF8
     34             //一次性读完
     35             QByteArray array = file.readAll();
     36             //显示到编辑区
     37             ui->textEdit->setText(QString::fromLocal8Bit(array));
     38             */
     39 
     40 
     41             //一行一行读
     42             QByteArray array;
     43             while( file.atEnd() == false)
     44             {
     45                 //读一行
     46                 array += file.readLine();
     47             }
     48             //显示到编辑区
     49             ui->textEdit->setText(QString::fromLocal8Bit(array));
     50 
     51         }
     52         file.close();
     53 
     54         //获取文件信息
     55         QFileInfo info(path);
     56         qDebug() << QString::fromLocal8Bit("文件名字:") <<info.fileName();
     57         qDebug() << QString::fromLocal8Bit("文件后缀:") <<info.suffix();
     58         qDebug() << QString::fromLocal8Bit("文件大小:") <<info.size();
     59         qDebug() << QString::fromLocal8Bit("文件创建时间:") <<
     60                     info.created().toString("yyyy-MM-dd hh:mm:ss");
     61 
     62     }
     63 }
     64 
     65 void MainWindow::on_buttonWrite_clicked()
     66 {
     67     QString path = QFileDialog::getSaveFileName(this,"save","../","TXT(*.txt)");
     68     if(path.isEmpty() == false)//不为空
     69     {
     70         QFile file;//创建文件对象
     71         //关联文件名字
     72         file.setFileName(path);
     73         //打开文件,只写方式
     74         bool isOK = file.open(QIODevice::WriteOnly);
     75         if(isOK == true)
     76         {
     77             //获取编辑区内容
     78             QString str = ui->textEdit->toPlainText();
     79             //写文件
     80             //把QString转换成QByteArray
     81             //file.write(str.toUtf8());
     82 
     83             //QString -> C++ string -> char *
     84             //file.write(str.toStdString().data());
     85 
     86             //把QString转换成本地编码
     87             file.write(str.toLocal8Bit());
     88 
     89 
     90             //字符类型的转换
     91             //把QString转换成QByteArray
     92             QString string = "123";
     93             QByteArray a = string.toUtf8();//主要是针对中文
     94             a = string.toLocal8Bit();//本地编码
     95             //QByteArray -> char *
     96             char *b = a.data();
     97             //char * -> QString
     98             char *p = "abc";
     99             QString c = QString(p);
    100 
    101         }
    102         file.close();
    103 
    104 
    105     }
    106 }

    三、QDataStream、QTextStream、QBuffer类操作文件

      (1)QDataStream:二进制方式操作数据流
      (2)QTextStream:文本方式操作数据流
      (3)QBuffer:内存文件

    代码如下:

    mainwindow.h

     1 #ifndef MAINWINDOW_H
     2 #define MAINWINDOW_H
     3 
     4 #include <QMainWindow>
     5 
     6 namespace Ui {
     7 class MainWindow;
     8 }
     9 
    10 class MainWindow : public QMainWindow
    11 {
    12     Q_OBJECT
    13 
    14 public:
    15     explicit MainWindow(QWidget *parent = 0);
    16     ~MainWindow();
    17 
    18     void writeData();
    19     void readData();
    20 
    21     void buffer();
    22 
    23 private:
    24     Ui::MainWindow *ui;
    25 };
    26 
    27 #endif // MAINWINDOW_H

    mainwindow.cpp

      1 #include "mainwindow.h"
      2 #include "ui_mainwindow.h"
      3 #include <QDataStream>//二进制方式操作数据流
      4 #include <QTextStream>//文本方式操作数据流
      5 #include <QBuffer>//内存文件
      6 #include <QByteArray>
      7 #include <QFile>
      8 #include <QDebug>
      9 //宏定义,输出 代码所在位置 和 所在行数
     10 #define cout qDebug() << "[" << __FILE__ << ":" << __LINE__ << "]"
     11 
     12 MainWindow::MainWindow(QWidget *parent) :
     13     QMainWindow(parent),
     14     ui(new Ui::MainWindow)
     15 {
     16     ui->setupUi(this);
     17 
     18     writeData();
     19     readData();
     20 
     21     buffer();
     22 
     23 }
     24 
     25 MainWindow::~MainWindow()
     26 {
     27     delete ui;
     28 }
     29 void MainWindow::writeData()
     30 {
     31     //二进制方式操作数据流
     32     /*
     33     //创建文件对象
     34     QFile file("../test.txt");
     35     //打开文件,只写方式
     36     bool isOK = file.open(QIODevice::WriteOnly);
     37     if(isOK == true)
     38     {
     39         //创建数据流,和file文件关联
     40         //往数据流中输数据,相当于往文件里写数据
     41         QDataStream stream(&file);
     42         //二进制编码,所以写的文本文档打开后的内容是乱码
     43         stream << QString("")<<250;//往数据流输入数据,,是以二进制的形式输入
     44         file.close();
     45     }
     46     */
     47 
     48 
     49     //文本方式操作数据流
     50     QFile file;
     51     file.setFileName("../demo.txt");
     52     bool isOK = file.open(QIODevice::WriteOnly);
     53     if(isOK == true)
     54     {
     55         QTextStream stream(&file);
     56         //默认是平台编码,所以文本可以直接看到内容
     57         //可以指定编码
     58         stream.setCodec("UTF-8");
     59         stream << QString("")<<250;
     60         file.close();
     61     }
     62 
     63 }
     64 void MainWindow::readData()
     65 {
     66 
     67     //二进制方式操作数据流
     68     /*
     69     //创建文件对象
     70     QFile file("../test.txt");
     71     //打开文件,只读方式
     72     bool isOK = file.open(QIODevice::ReadOnly);
     73     if(isOK == true)
     74     {
     75         //创建数据流,和file文件关联
     76         //从数据流中读数据,相当于从文件里读数据
     77         QDataStream stream(&file);
     78         //读的时候,按写的顺序取数据
     79         QString str;
     80         int a;
     81         stream >> str >> a;
     82         qDebug() << str.toUtf8().data() << a ;
     83         cout << str.toUtf8().data() << a ;
     84 
     85         file.close();
     86     }
     87     */
     88 
     89 
     90     //文本方式操作数据流
     91     QFile file;
     92     file.setFileName("../demo.txt");
     93     bool isOK = file.open(QIODevice::ReadOnly);
     94     if(isOK == true)
     95     {
     96         QTextStream stream(&file);
     97         //默认是平台编码,所以文本可以直接看到内容
     98         //可以指定编码
     99         stream.setCodec("UTF-8");
    100 
    101         //此处读文件的时候,不需要象QDataStream一样先按照类型读
    102         QString str = stream.readAll();
    103         cout << str.toUtf8().data() ;
    104         file.close();
    105     }
    106 
    107 
    108 }
    109 
    110 void MainWindow::buffer()
    111 {
    112     QByteArray array;
    113     QBuffer memFile(&array);//将缓冲区的内容存到数组中
    114     //QBuffer memFile;//创建内存文件
    115     memFile.open(QIODevice::WriteOnly);
    116     memFile.write("11111111");//写到缓冲区
    117     memFile.write("22222222");
    118     memFile.close();
    119     qDebug() << memFile.buffer();//得到缓冲区的内容
    120     qDebug() << "array" << array;
    121 }
  • 相关阅读:
    Eureka的工作原理以及它与ZooKeeper的区别
    利用javascript判断文件是否存在
    带jsk证书,请求https接口
    C# .net 数组倒序排序
    C#中ArrayList和string,string[]数组的转换
    C#中遍历ArrayList的三种方法
    求其中同一个主叫号码的两次通话之间间隔大于10秒的通话记录ID
    启动tomcat时,一直卡在Deploying web application directory这块的解决方案
    Linux下修改Mysql的用(root的密码及修改root登录权限
    启动MySql提示:The server quit without updating PID file(…)失败
  • 原文地址:https://www.cnblogs.com/blog-ccs/p/7453381.html
Copyright © 2020-2023  润新知