文件类QFile
使用QFile操作文件的一般流程:
- 创建QFile对象,指定文件路径
- 打开文件
- 读/写文件
- 关闭文件,读/写完之后一定要记得关闭
常用方法
构造函数
直接用字符串指定要操作的文件的文件路径
QFile::QFile(const QString &name)
打开文件
[override virtual] bool QFile::open(QIODeviceBase::OpenMode mode)
打开失败返回false,打开成功返回true
mode的常用的几种取值如下(取值可以用 | 连接):
- QIODevice::ReadOnly //以只读的方式打开(文件不存在出错)
- QIODevice::WriteOnly //以只写的方式打开,打开时文件内原内容清空(文件不存在则创建)(默认是Truncate)
- QIODevice::ReadWrite //以读写的方式打开文件(默认是Truncate)
- QIODevice::Append //已追加的方式打开文件,新写入文件的数据添加到文件尾部
- QIODevice::Truncate //以重写的方式打开文件,打开文件时原有内容会先清空
- QIODevice::Text //以文本的方式打开文件。读取时,将'\n'翻译为换行符;写入时,将行结束符转换成本地格式(如 Windows 下是'\r\n')
- QIODevice:::ExistingOnly //则文件不存在时打开失败,并且不会创建
- QIODevice::NewOnly //文件存在时打开失败,只有文件不存在时才会打开成功且创建,来保证用户是唯一创建者
打开失败错误码
上面的文件打开失败时,可以获取到错误码
QFileDevice::FileError QFileDevice::error() const
打开失败错误提示
上面的文件打开失败时,可以获取到具体的错误信息
QString QIODevice::errorString() const
关闭文件
[override virtual] void QFileDevice::close()
写文件
//方法1比较常用,直接将一个char*字符串写入到文件中
qint64 QIODevice::write(const char *data)
qint64 QIODevice::write(const QByteArray &data)
qint64 QIODevice::write(const char *data, qint64 maxSize)
判断是否到达文件尾部
[override virtual] bool QFileDevice::atEnd() const
将文件指针定位到指定位置处
步长是1个字节,比如pos为5,则文件指针定位到第5个字节处
[override virtual] bool QFileDevice::seek(qint64 pos)
获取文件指针的当前位置
[override virtual] qint64 QFileDevice::pos() const
获取打开文件的文件大小
[override virtual] qint64 QFile::size() const
按行读取版本1
//参数:这一行最多读取多少个字节。不指定时,直接读取一整行
//返回:读取的内容,存在1个QByteArray里面
QByteArray QIODevice::readLine(qint64 maxSize = 0)
举例
void ReadFile01()
{QFile file("./aa.txt");if(file.open(QIODevice::ReadWrite)){while (!file.atEnd()) {QByteArray buffer=file.readLine();qDebug()<<buffer.constData();}file.close();}else{qDebug()<<file.error();qDebug()<<file.errorString();}
}
按行读取版本2
//参数1:缓冲区
//参数2:缓冲区的大小
//返回:实际读出的字节数,返回0表示没有数据可读了,返回-1表示读取出错
qint64 QIODevice::readLine(char *data, qint64 maxSize)
举例
void ReadFile02()
{QFile file("./aa.txt");if(file.open(QIODevice::ReadWrite)){char buffer[1024]={0};int ret=file.readLine(buffer,sizeof(buffer));while(ret!=-1 && ret!=0){qDebug()<<buffer;ret=file.readLine(buffer,sizeof(buffer));}file.close();}else{qDebug()<<file.error();qDebug()<<file.errorString();}
}
一次性读取所有
适合size较小的文件
QByteArray QIODevice::readAll()
举例
void ReadFile03()
{QFile file("./aa.txt");if(file.open(QIODevice::ReadWrite)){QByteArray d=file.readAll();qDebug()<<d.constData();file.close();}else{qDebug()<<file.error();qDebug()<<file.errorString();}
}
按字节读取
和按行读取使用方法一样
QByteArray QIODevice::read(qint64 maxSize)
qint64 QIODevice::read(char *data, qint64 maxSize)
复制文件
bool QFile::copy(const QString &newName)
newName可以是文件名:bb.txt,也可以是一个相对或绝对路径:../bb.txt
判断文件是否存在
bool QFile::exists() const//这个静态方法也可以判断
[static] bool QFile::exists(const QString &fileName)
创建连接(快捷方式)
bool QFile::link(const QString &linkName)
删除当前文件
bool QFile::remove()
删除指定文件
//静态方法
[static] bool QFile::remove(const QString &fileName)
重命名
bool QFile::rename(const QString &newName)
[static] bool QFile::rename(const QString &oldName, const QString &newName)
学习链接:https://github.com/0voice