1 簡介
參考視頻:https://www.bilibili.com/video/BV1XW411x7NU?p=45
參考文檔:《Qt教程.docx》
說明:本文主要介紹Qt的文件系統。
2 知識點
Qt通過QIODevice提供了對 I/O 設備的抽象,這些設備具有讀寫字節塊的能力。下面是 I/O 設備的類圖(Qt5):
-
QIODevice:所有 I/O 設備類的父類,提供了字節塊讀寫的通用操作以及基本接口;
-
QFileDevice:Qt5新增加的類,提供了有關文件操作的通用實現。
-
QFlie:訪問本地文件或者嵌入資源;
-
QTemporaryFile:創建和訪問本地文件系統的臨時文件;
-
QBuffer:讀寫QbyteArray, 內存文件;
-
QProcess:運行外部程序,處理進程間通訊;
-
QAbstractSocket:所有套接字類的父類;
-
QTcpSocket:TCP協議網絡數據傳輸;
-
QUdpSocket:傳輸 UDP 報文;
-
QSslSocket:使用 SSL/TLS 傳輸數據;
3 基本文件操作
QFile提供了從文件中讀取和寫入數據的能力。
我們通常會將文件路徑作為參數傳給QFile的構造函數。不過也可以在創建好對象最后,使用setFileName()來修改。
QFile主要提供了有關文件的各種操作,比如打開文件、關閉文件、刷新文件等。我們可以使用QDataStream或QTextStream類來讀寫文件,也可以使用QIODevice類提供的read()、readLine()、readAll()以及write()這樣的函數。
值得注意的是,有關文件本身的信息,比如文件名、文件所在目錄的名字等,則是通過QFileInfo獲取,而不是自己分析文件路徑字符串。
4 測試
(1)打開、讀取、寫入文件
功能:創建兩個按鈕,“read file”按鈕打開一個文本框,讀取文件的內容到textEdit;“save file”按鈕將textEdit中的內容保存到一個文件中。界面如下:
讀取文件使用到的函數和步驟:
1)創建一個文件對象:QFilefile(path);
2)以只讀方式打開文件:boolisOk=file.open(QIODevice::ReadOnly);
3)讀取文件的內容有多種方式,這里介紹兩種:
一次性讀取文件全部內容:QByteArrayarray=file.readAll();
一次只讀取一行:
1 QByteArray array;
2 while (file.atEnd() == false) {
3 //讀一行
4 array += file.readLine();
5 }
4)關閉文件:file.close();
寫入文件用到的函數和步驟:
1)創建文件對象:QFile file;
2)關聯文件名字:file.setFileName(path);
3)以只寫方式打開文件:bool isOK = file.open(QIODevice::WriteOnly);
4)向文件中寫入內容:file.write(str);
5)關閉文件:file.close();
完整代碼:

1 #include "widget.h"
2 #include "ui_widget.h"
3 #include <QFile>
4 #include <QFileDialog>
5 #include <QByteArray>
6 #include <QByteArray>
7
8 Widget::Widget(QWidget *parent) : 9 QWidget(parent), 10 ui(new Ui::Widget) 11 { 12 ui->setupUi(this); 13 } 14
15 Widget::~Widget() 16 { 17 delete ui; 18 } 19
20 void Widget::on_pushButton_read_clicked() 21 { 22 QString path = QFileDialog::getOpenFileName(this, "open", "../", "TXT(*.txt)"); 23 if (path.isEmpty() == false) { 24 QFile file(path); //文件對象
25 bool isOK = file.open(QIODevice::ReadOnly); //打開文件
26 if (true == isOK) { 27 //一行一行讀取文件
28 QByteArray array; 29 while (file.atEnd() == false) { 30 array += file.readLine(); 31 } 32 ui->textEdit->setText(array); 33 file.close(); 34 } 35 } 36 } 37
38 void Widget::on_pushButton_save_clicked() 39 { 40 QString path = QFileDialog::getOpenFileName(this, "open", "../", "TXT(*.txt)"); 41 if (path.isEmpty() == false) { 42 QFile file(path); //文件對象
43 bool isOK = file.open(QIODevice::WriteOnly); //打開文件
44 if (true == isOK) { 45 //獲取文本編輯區的內容
46 QString str = ui->textEdit->toPlainText(); 47 //轉換為本地編碼寫入
48 file.write(str.toLocal8Bit()); 49 file.close(); 50 } 51 } 52 }
運行測試:
上述代碼中,打開文件使用的是系統默認的編碼格式:UTF-8,如果打開其它編碼格式的文件就會亂碼。
(2)獲取文件信息
比如獲取文件的名字、文件后綴、文件大小、文件創建時間等信息。
通過QFileInfo類來實現的。這個很簡單,直接給出測試代碼:

1 Widget::Widget(QWidget *parent) : 2 QWidget(parent), 3 ui(new Ui::Widget) 4 { 5 ui->setupUi(this); 6
7 QFileInfo info("../demo.txt"); 8 qDebug() << "文件名字" << info.fileName().toUtf8().data(); 9 qDebug() << "文件后綴" << info.suffix(); 10 qDebug() << "文件大小" << info.size(); 11 qDebug() << "文件創建時間" << info.created().toString("yyyy-MM-dd hh:mm:ss"); 12 qDebug() << "文件路徑" << info.filePath(); 13 }
(3)QDataStream讀寫文件
QDataStream提供了基於QIODevice的二進制數據的序列化。數據流是一種二進制流,這種流完全不依賴於底層操作系統、CPU 或者字節順序(大端或小端)。
QDataStream既能夠存取 C++ 基本類型,如 int、char、short 等,也可以存取復雜的數據類型,例如自定義的類。實際上,QDataStream對於類的存儲,是將復雜的類分割為很多基本單元實現的。
測試代碼:創建兩個函數,writeData()寫文件,readData()讀文件。

1 #include "widget.h"
2 #include "ui_widget.h"
3 #include <QDataStream>
4 #include <QFile>
5 #include <QDebug>
6
7 #define cout qDebug() << "[" << __FILE__ <<":" << __LINE__ << "]"
8
9
10 Widget::Widget(QWidget *parent) : 11 QWidget(parent), 12 ui(new Ui::Widget) 13 { 14 ui->setupUi(this); 15
16 writeData(); 17 readData(); 18 } 19
20 Widget::~Widget() 21 { 22 delete ui; 23 } 24
25 void Widget::writeData() 26 { 27 //創建文件對象
28 QFile file("../text.txt"); 29 //打開文件,只寫方式
30 bool isOK = file.open(QIODevice::WriteOnly); 31 if (true == isOK) { 32 //創建數據流,和file文件關聯 33 //往數據流中寫數據,相當於往文件里寫數據(二進制)
34 QDataStream stream(&file); 35 stream << QString("主要看氣質") << 250; 36 file.close(); 37 } 38 } 39
40 void Widget::readData() 41 { 42 //創建文件對象
43 QFile file("../text.txt"); 44 //打開文件,只讀方式
45 bool isOK = file.open(QIODevice::ReadOnly); 46 if (true == isOK) { 47 //創建數據流,和file文件關聯 48 //從數據流中讀數據,相當於從文件里讀數據(二進制)
49 QDataStream stream(&file); 50 //讀的時候,按寫的順序取數據
51 QString str; 52 int a; 53 stream >> str >> a; 54 cout << str << a; 55 file.close(); 56 } 57 }
運行測試:
打印輸出:[ ..\file_2\widget.cpp : 54 ] "主要看氣質" 250
我們打開text.txt文件,會看到是亂碼的,因為是二進制文件。
(4)QTextStream讀寫文件
QTextStream是操作純文本文件的。QTextStream會自動將 Unicode 編碼同操作系統的編碼進行轉換,這一操作對開發人員是透明的。它也會將換行符進行轉換,同樣不需要自己處理。
QTextStream使用 16 位的QChar作為基礎的數據存儲單位,同樣,它也支持 C++ 標准類型,如 int 等。實際上,這是將這種標准類型與字符串進行了相互轉換。
測試代碼:

1 #include "widget.h"
2 #include "ui_widget.h"
3 #include <QFile>
4 #include <QTextStream>
5 #include <QDebug>
6 #include <QFileDialog>
7
8 #define cout qDebug() << "[" << __FILE__ <<":" << __LINE__ << "]"
9
10 Widget::Widget(QWidget *parent) : 11 QWidget(parent), 12 ui(new Ui::Widget) 13 { 14 ui->setupUi(this); 15
16 writeData(); 17 readData(); 18 } 19
20 Widget::~Widget() 21 { 22 delete ui; 23 } 24
25 void Widget::writeData() 26 { 27 QFile file; 28 file.setFileName("../demo.txt"); 29 bool isOK = file.open(QIODevice::WriteOnly); 30 if (true == isOK) { 31 QTextStream stream(&file); 32 //指定編碼
33 stream.setCodec("UTF-8"); 34 stream << QString("主要看氣質") << 250; 35 file.close(); 36 } 37 } 38
39 void Widget::readData() 40 { 41 QFile file; 42 file.setFileName("../demo.txt"); 43 bool isOK = file.open(QIODevice::ReadOnly); 44 if (true == isOK) { 45 QTextStream stream(&file); 46 stream.setCodec("UTF-8"); 47 //讀取出來的就是字符串
48 QString str = stream.readAll(); 49 cout << str; 50 file.close(); 51 } 52 }
運行測試:
打印輸出:[ ..\file_3\widget.cpp : 53 ] "主要看氣質250"
打開demo.txt文件,可以發現文件是可讀的,不會亂碼。
(5)QBuffer讀寫文件
QBuffer創建的是內存文件。
直接給出測試代碼:

1 Widget::Widget(QWidget *parent) : 2 QWidget(parent), 3 ui(new Ui::Widget) 4 { 5 ui->setupUi(this); 6 7 //創建內存文件 8 QByteArray array; 9 QBuffer memfile(&array); 10 memfile.open(QIODevice::WriteOnly); 11 memfile.write("111111111111"); 12 memfile.write("222222222222"); 13 memfile.close(); 14 qDebug() << memfile.buffer(); 15 qDebug() << "array" << array; 16 17 QBuffer memfile1; 18 QDataStream stream(&memfile1); 19 memfile1.open(QIODevice::WriteOnly); 20 stream << QString("測試") << 250; 21 memfile1.close(); 22 23 memfile1.open(QIODevice::ReadOnly); 24 QDataStream in; 25 in.setDevice(&memfile1); 26 QString str; 27 int a; 28 in >> str >> a; 29 memfile1.close(); 30 qDebug() << str.toUtf8().data() << a; 31 }