Qt Creator模块学习——文件,目录和输入/输出(一)
一. 文件和目录
1. 输入/输出设备
QIODevice类是Qt中所有I/0设备的基础接口类,例如QFile,QBuffer和QTcpSocket等支持读/写数据块的设备提供了一个抽象接口。
在整个流程当中,我们往往通过open()函数打开该设备,而且必须指定正确的打开模式。QIODevice中所有的打开模式都是由QIODevice::OpenMode枚举类型定义,其取值大家可以自行查找,太多了…
咱们只拿几个举例
常量 | 描述 |
---|---|
QIODevice::NotOpen | 设备没有打开 |
QIODevice::ReadOnly | 设备以只读方式打开,不可写 |
QIODevice::WriteOnly | 设备以只写方式打开,不可读 |
QIODevice::ReadWrite | 以读写方式打开 |
QIODevice::Append | 以附加模式打开(在文件末尾) |
QIODevice::Truncate | 如果可以的话,设备在打开前会被截断,设备先前的内容也都会丢失 |
QIODevice::Text | 读取时,行结尾终止符被转化为"/n",当写入时行结尾终止符会被转为本地编码 |
QIODevice::Unbuffered | 绕过设备所有的缓冲区 |
在进行写入读取后,通过close()关闭设备。
QIODevice可以对设备进行区别,区别成顺序存储设备和随机存储设备。
随机存储设备可以通过seek()函数定义任意位置,当前位置也可以用pos()函数获取。用于QFile,QBuffer。
顺序存储设备不能定位任意区域,数据必须一次读取用于QTcpStock,QProcess。
在程序中isSequential()函数判断设备类型。
QTcpStock,QProcess是两个大模块,我们现在要说的是QFile。
2.文件操作
QFile类提供了读/写设备的接口,它可以读/写文本文件,二进制文件和Qt资源的I/O设备。QFile可以单独使用,但是为了方便我们往往和QTextStream/QDataStream结合使用。
QFileInfo类是提供与系统无关的文件信息,如大小名字位置等。
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QStringList>
#include <QCoreApplication>
#include <QFileInfo>
#include <QDebug>
#include <QDateTime>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//以只写方式打开,若文件不存在则创建
QFile file("file.txt");
if(!file.open(QIODevice::WriteOnly|QIODevice::Text))
qDebug()<<file.errorString();
file.write("hello,Qt \angxn!!");
file.close();
QFileInfo info(file);
qDebug()<<QObject::tr("绝对路径:")<<info.absoluteFilePath()<<endl
<<QObject::tr("文件名:")<<info.fileName()<<endl
<<QObject::tr("基本名称:")<<info.baseName()<<endl
<<QObject::tr("后缀:")<<info.suffix()<<endl
<<QObject::tr("创建时间:")<<info.created()<<endl
<<QObject::tr("大小:")<<info.size();
//以只读方式打开
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
qDebug()<<file.errorString();
qDebug()<<QObject::tr("文件内容:")<<endl<<file.readAll();
qDebug()<<QObject::tr("当前位置:")<<file.pos();
file.seek(0);
QByteArray array;
array = file.read(5);
qDebug()<<QObject::tr("前五个字符:")<<array
<<QObject::tr("当前位置:")<<file.pos();
file.seek(15);
array = file.read(5);
qDebug()<<QObject::tr("第16-20个字符")<<array;
file.close();
return a.exec();
}
运行结果如下:
3.目录操作
QDir类用来访问目录结构及其内容,还可以访问操作路径名,访问路径和文件的相关信息等。
下面我们举个例子:
在mainwindow.ui里拖入一个List Widget部件
随后在.h文件中加入私有对象:
#include <QFileSystemWatcher>
private:
QFileSystemWatcher myWatcher;
private slots:
void showMessage(const QString &path);
main.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&myWatcher,&QFileSystemWatcher::directoryChanged,
this,&MainWindow::showMessage);
connect(&myWatcher,&QFileSystemWatcher::fileChanged,
this,&MainWindow::showMessage);
//显示当前目录下所有文件
QDir mydir(QDir::currentPath());
mydir.setNameFilters(QStringList("*h"));
ui->listWidget->addItem(mydir.absolutePath()+ tr("目录下的.h文件"));
ui->listWidget->addItems(mydir.entryList());
//创建目录加入显示器
mydir.mkdir("mydir");
mydir.cd("mydir");
ui->listWidget->addItem(tr("监视目录:")+mydir.absolutePath());
myWatcher.addPath(mydir.absolutePath());
//创建文件加入监视器
QFile file(mydir.absolutePath()+"/myfile.txt");
if(file.open(QIODevice::WriteOnly)){
QFileInfo info(file);
ui->listWidget->addItem(tr("监视的文件:")+ info.absolutePath());
myWatcher.addPath(info.absoluteFilePath());
file.close();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showMessage(const QString &path){
QDir dir(QDir::currentPath()+"/mydir");
//如果目录发生改变
if(path==dir.absolutePath()){
ui->listWidget->addItem(dir.dirName()+tr("目录发生改变:"));
ui->listWidget->addItems(dir.entryList());
}
else{
ui->listWidget->addItem(path+tr("文件发生改变"));
}
}
大家可以自行感受。