写文件
QFile file("filename.bin");
file.open(QIODevice::WriteOnly);
file.write(...);
file.close();
qint64 write(const char *data, qint64 len);
qint64 write(const char *data);
qint64 write(const QByteArray &data);
读文件
QFile file("filename.bin");
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
file.close();
qint64 read(char *data, qint64 maxlen);
QByteArray read(qint64 maxlen);
QByteArray readAll();
qint64 readLine(char *data, qint64 maxlen);
QByteArray readLine(qint64 maxlen = 0);
选择文件
QString filename;
filename = QFileDialog::getOpenFileName(this, "打开", "D:\", "图片文件(*.bmp *.jpg);;所有文件(*.*)");
filename = QFileDialog::getSaveFileName(this, "保存为", "D:\", "文本文档(*.txt);;所有文件(*.*)");
将函数的目录参数"D:\"
改成NULL
,首次打开文件选择窗口时默认为程序所在目录,后续打开文件选择就为上一次操作所在目录。
选择目录
QDir dirPath;
dirPath = QFileDialog::getExistingDirectory( this, "选择文件夹", "D:\");
文件信息
QFileInfo info(name);
bool exists = info.exists();
bool isDir = info.isDir();
bool isFile= info.isFile();
bool isRead= info.isReadable();
bool isWrite=info.isWritable();
bool isExec= info.isExecutable();