#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <QByteArray>
#include <QDebug>
#include <QDateTime>
#include <QObject>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//
QFile file("test.txt");
if(!file.open(QIODevice::WriteOnly|QIODevice::Text))
{
qDebug() << QObject::tr("打开文件失败!");
return 0;
}
file.write("I like apple!");
file.close();
QFileInfo fileInfo(file);
qDebug() << fileInfo.absoluteFilePath();
qDebug() << fileInfo.fileName();
qDebug() << fileInfo.baseName();
qDebug() << fileInfo.suffix();
qDebug() << fileInfo.created();
qDebug() << fileInfo.size();
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
{
qDebug() << QObject::tr("打开文件失败!");
return 0;
}
qDebug() << file.readAll();
qDebug() << file.pos();
file.seek(0);
qDebug() << file.read(12);
qDebug() << file.pos();
return a.exec();
}
参考:
不二如是:https://fishc.com.cn/forum.php?mod=viewthread&tid=78173&ctid=447