文件与IO系统
# 03.文件与IO系统
# 目录介绍
- 3.1 文件与IO系统
- 3.1.1 文件操作类
- 3.1.2 数据流类
- 3.1.3 资源系统
- 3.2 文件读写
- 3.2.1 QFile类读写
- 3.2.2 QTextStream读写
- 3.2.3 QDataStream读写
- 3.3 目录管理
- 3.3.1 QDir类
- 3.3.2 QFileInfo类
- 3.4 文件监控
- 3.4.1 QFileSystemWatcher
- 3.5 临时文件
- 3.5.1 QTemporaryFile
# 3.1 文件与IO系统
功能:处理文件、目录、输入输出流。
核心类:
QFile、QDir:文件和目录操作。QTextStream、QDataStream:文本和二进制流。QIODevice:所有 I/O 设备的基类。QFileInfo:用于获取文件信息。QFileSystemWatcher:用于监控文件或目录的变化。QTemporaryFile:用于创建临时文件。
# 3.1.1 文件操作类
- QFile:文件读写
- QDir:目录操作
- QFileInfo:文件信息
- QFileSystemWatcher:文件系统监控
# 3.1.2 数据流类
- QDataStream:二进制数据序列化
- QTextStream:文本数据流
- QBuffer:内存缓冲区
# 3.1.3 资源系统
- QResource:访问 Qt 资源系统(.qrc)
- QSettings:跨平台应用程序设置存储
# 3.2 文件读写
# 3.2.1 QFile类读写
QFile 是 Qt 中用于文件读写的核心类。
1.打开文件
QFile file("example.txt");
if (!file.open(QIODevice::ReadWrite)) {
qDebug() << "Failed to open file!";
return;
}
1
2
3
4
5
2
3
4
5
QIODevice::ReadOnly:只读模式。QIODevice::WriteOnly:只写模式。QIODevice::ReadWrite:读写模式。QIODevice::Append:追加模式。
2.读取文件
QByteArray data = file.readAll(); // 读取全部内容
qDebug() << data;
QString line;
while (!file.atEnd()) {
line = file.readLine(); // 逐行读取
qDebug() << line;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
3.写入文件
file.write("Hello, Qt!"); // 写入数据
file.write("\nNew line");
1
2
2
4.关闭文件
file.close();
1
# 3.2.2 QTextStream读写
QTextStream 提供了更高级的文本读写功能。
1.读取文本
QFile file("example.txt");
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open file!";
return;
}
QTextStream in(&file);
QString text = in.readAll(); // 读取全部内容
qDebug() << text;
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
2.写入文本
QFile file("example.txt");
if (!file.open(QIODevice::WriteOnly)) {
qDebug() << "Failed to open file!";
return;
}
QTextStream out(&file);
out << "Hello, Qt!" << Qt::endl;
out << "This is a new line.";
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 3.2.3 QDataStream读写
QDataStream 用于二进制数据的读写。
1.写入二进制数据
QFile file("data.bin");
if (!file.open(QIODevice::WriteOnly)) {
qDebug() << "Failed to open file!";
return;
}
QDataStream out(&file);
out << QString("Hello, Qt!") << qint32(123);
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
2.读取二进制数据
QFile file("data.bin");
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open file!";
return;
}
QDataStream in(&file);
QString text;
qint32 number;
in >> text >> number;
qDebug() << text << number;
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 3.3 目录管理
# 3.3.1 QDir类
QDir 用于目录操作。
1.创建目录
QDir dir("new_directory");
if (!dir.exists()) {
dir.mkpath("."); // 创建目录
}
1
2
3
4
2
3
4
2.遍历目录
QDir dir(".");
QStringList files = dir.entryList(QDir::Files); // 获取文件列表
foreach (QString file, files) {
qDebug() << file;
}
1
2
3
4
5
2
3
4
5
3.删除目录
QDir dir("old_directory");
if (dir.exists()) {
dir.removeRecursively(); // 递归删除目录
}
1
2
3
4
2
3
4
# 3.3.2 QFileInfo类
QFileInfo 用于获取文件或目录的详细信息。
1.获取文件信息
QFileInfo info("example.txt");
qDebug() << "File size:" << info.size();
qDebug() << "Last modified:" << info.lastModified();
qDebug() << "Is file?" << info.isFile();
1
2
3
4
2
3
4
# 3.4 文件监控
# 3.4.1 QFileSystemWatcher
QFileSystemWatcher 用于监控文件或目录的变化。
1.监控文件
QFileSystemWatcher watcher;
watcher.addPath("example.txt");
QObject::connect(&watcher, &QFileSystemWatcher::fileChanged, [](const QString &path) {
qDebug() << "File changed:" << path;
});
1
2
3
4
5
2
3
4
5
2.监控目录
QFileSystemWatcher watcher;
watcher.addPath(".");
QObject::connect(&watcher, &QFileSystemWatcher::directoryChanged, [](const QString &path) {
qDebug() << "Directory changed:" << path;
});
1
2
3
4
5
2
3
4
5
# 3.5 临时文件
# 3.5.1 QTemporaryFile
QTemporaryFile 用于创建临时文件。
QTemporaryFile tempFile;
if (tempFile.open()) {
tempFile.write("Temporary data");
qDebug() << "Temp file path:" << tempFile.fileName();
tempFile.close();
}
1
2
3
4
5
6
2
3
4
5
6
上次更新: 2026/06/10, 11:13:41