qt_文件管理
QDir,QFile
QDir
获取路径和进入文件夹,删除文件,文件夹
枚举
enum Filter { Dirs, AllDirs, Files, Drives, …, CaseSensitive }
flags Filters
enum SortFlag { Name, Time, Size, Type, …, LocaleAware }
flags SortFlags
函数 |
描述 |
QString absoluteFilePath(const QString &fileName) const |
返回文件的绝对位置+文件名,不检查是否存在,exist去检查就好 |
QString absolutePath() const |
QDir的绝对路径 |
QString dirName() const |
返回dir路径名 |
QString filePath(const QString &fileName) const |
返回路径名 |
QString path() const |
路径 |
QString relativeFilePath(const QString &fileName) const |
返回路径名 |
int count()const |
返回的是该目录下文件夹和文件的数量+./+../ |
QFileInfoList entryInfoList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const |
每一个文件或者文件的对象列表 |
QStringList entryList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const |
文件和目录的名称列表 |
QDir::SortFlags sorting() const |
返回排序的规律 |
QDir home() |
|
QString homePath() |
|
QDir root() |
|
QString rootPath() |
|
QDir temp() |
|
QString tempPath() |
|
判断
函数 |
描述 |
bool isAbsolute() const |
绝对位置 |
bool isEmpty(QDir::Filters filters = Filters(AllEntries |
NoDotAndDotDot)) const |
bool isReadable() const |
可读 |
bool isRelative() const |
相对位置 |
bool isRoot() const |
根目录 |
bool exists(const QString &name) const |
|
bool exists() const |
存在 |
操作
函数 |
描述 |
bool cd(const QString &dirName) |
|
bool cdUp() |
|
bool mkdir(const QString &dirName) const |
创建目录 |
bool mkpath(const QString &dirPath) const |
创建一长串的目录 |
void refresh() const |
刷新 |
bool remove(const QString &fileName) |
删除文件 |
bool rename(const QString &oldName, const QString &newName) |
重命名 |
bool rmdir(const QString &dirName) const |
删除目录 |
bool rmpath(const QString &dirPath) const |
删除多个目录 |
void setSorting(QDir::SortFlags sort) |
设置排序 |
void setFilter(QDir::Filters filters) |
设置过滤器 |
void setNameFilters(const QStringList &nameFilters) |
|
void setPath(const QString &path) |
设置path |
FileIo::FileIo(QWidget *parent)
: QWidget(parent)
{
setWindowTitle("file system");
QDir file_dir("E:/qttest/testqrc");
if(file_dir.exists("hz1.jpg"))
qDebug()<<file_dir.absoluteFilePath("hz1.jpg");
allFile("E:/newfile1");
rmAll("E:/newfile1");
}
QFileInfo
提供一系列文件信息,前缀,后缀,大小,创建时间…..相当于文件的属性,详细信息
函数 |
描述 |
QDir absoluteDir() const |
绝对位置的Dir |
QDir dir() const |
返回dir |
QString absoluteFilePath() const |
路径名 |
QString path() const |
路径名 |
QString absolutePath() const |
|
QString baseName() const |
名称第一部分 |
QString suffix() const |
文件后缀 |
qint64 size() const |
大小 |
QDateTime birthTime() const |
创建时间 |
QDateTime lastModified() const |
最后修改时间 |
QDateTime lastRead() const |
最后访问时间 |
QDateTime metadataChangeTime() const |
比如权限被修改时间 |
bool permission(QFile::Permissions permissions) const |
是否具有文件权限 |
QFile::Permissions permissions() const |
文件的权限 |
QString owner() const |
文件属于谁 |
uint ownerId() const |
文件主的id |
QString bundleName() const |
包名称 |
bool caching() const |
是否为缓存 |
QString canonicalFilePath() const |
链接路径 |
QString canonicalPath() const |
链接路径 |
QString completeBaseName() const |
文件名称,去掉后缀 |
QString completeSuffix() const |
文件后缀,去掉第一部分也就是文件名称 |
bool exists() const |
判端是否存在 |
QString fileName() const |
文件的名称 |
QString filePath() const |
文件的路径 |
QString group() const |
文件的组名称,unix里面 |
uint groupId() const |
文件的组的id |
bool isAbsolute() const |
是否为绝对路径 |
bool isBundle() const |
是否为包 |
bool isExecutable() const |
可执行 |
bool isReadable() const |
可读 |
bool isWritable() const |
可写 |
bool isFile() const |
文件,而不是目录 |
bool isDir() const |
是否为目录 |
bool isHidden() const |
隐藏文件 |
bool isNativePath() const |
本地文件,而不是资源文件下的东西 |
bool isRelative() const |
相对文件,在资源文件下,或者默认目录下的 |
bool isRoot() const |
根 |
bool isSymLink() const |
是否链接文件 |
QString symLinkTarget() const |
链接文件的名字 |
void refresh() |
刷新文件的信息 |
void setCaching(bool enable) |
设为缓存 |
void setFile(const QString &file) |
设置文件 |
void setFile(const QFile &file) |
|
void setFile(const QDir &dir, const QString &file) |
|
void readAllFile(const QString & path)
{
QTextStream cout(stdout);
cout.setFieldWidth(20);
cout<<endl;
cout.setFieldAlignment(QTextStream::AlignLeft);
QDir dir(path);
if (!dir.exists())
return ;
QFileInfoList list = dir.entryInfoList(QDir::Files|QDir::Dirs,QDir::Time);
int i = 0;
do{
QFileInfo fileInfo = list.at(i);
if(fileInfo.fileName()=="." | fileInfo.fileName()=="..")
{
i++;
continue;
}
bool bisDir = fileInfo.isDir();
if(bisDir)
{
readAllFile(fileInfo.filePath());
}
else
{
cout<<fileInfo.path()<<fileInfo.fileName()<<fileInfo.created().toString
("yyyy-MM-d h:m:s")<<fileInfo.size()/(1024.0)<<"\n";
}
i++;
}while(i < list.size());
}
void rmAllFile(const QString &path)
{
QDir dir(path);
if (!dir.exists())
return ;
QFileInfoList list = dir.entryInfoList(QDir::Files|QDir::Dirs,QDir::Time);
int i = 0;
do{
QFileInfo fileInfo = list.at(i);
if(fileInfo.fileName()=="." | fileInfo.fileName()=="..")
{
i++;
continue;
}
bool bisDir = fileInfo.isDir();
if(bisDir)
{
rmAllFile(fileInfo.filePath());
}
else
{
dir.remove(fileInfo.absoluteFilePath());
}
i++;
}while(i < list.size());
dir.rmpath(path);
}
QFile
函数 |
描述 |
bool copy(const QString &newName) |
赋值 |
bool link(const QString &linkName) |
链接 |
bool remove() |
删除 |
bool open(FILE *fh, QIODevice::OpenMode mode, QFileDevice::FileHandleFlags handleFlags = DontCloseHandle) |
打开 |
void setFileName(const QString &name) |
设置名字 |
bool rename(const QString &oldName, const QString &newName) |
重命名 |
QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
while (!file.atEnd()) {
QByteArray line = file.readLine();
process_line(line);
}
FileIo::FileIo(QWidget *parent)
: QWidget(parent)
{
setWindowTitle("file system")
QFile file("E:/1.txt")
QTextStream out(&file)
QTextStream cout(stdout)
file.open(QFile::ReadWrite|QFile::Text)
out<<"myname is zyldcsdfcsdfcsdg"<<endl
cout<<file.readAll()
file.close()
}
QTextStream
text的流,相同的data流也存在
函数 |
描述 |
bool atEnd() const |
流指针是否在末尾 |
qint64 pos() const |
流指针位置 |
QString read(qint64 maxlen) |
读文件 |
QString readAll() |
读文件 |
QString readLine(qint64 maxlen = 0) |
读文件 |
void reset() |
充值流默认设置 |
void resetStatus() |
重置流状态 |
bool seek(qint64 pos) |
跳转 |
void setCodec(QTextCodec *codec) |
设置编码格式 |
void setCodec(const char *codecName) |
|
void setDevice(QIODevice *device) |
设置设备 |
void setFieldAlignment(QTextStream::FieldAlignment mode) |
对齐方式 |
void setFieldWidth(int width) |
宽度 |
void setGenerateByteOrderMark(bool generate) |
会插入bom标记 |
void setIntegerBase(int base) |
进制设置 |
void setNumberFlags(QTextStream::NumberFlags flags) |
设置标志 |
void setPadChar(QChar ch) |
空白填充 |
void setRealNumberPrecision(int precision) |
设置小数的位数 |
void setStatus(QTextStream::Status status) |
流状态 |
status:
函数 |
值 |
描述 |
QTextStream::Ok |
0 |
The text stream is operating normally. |
QTextStream::ReadPastEnd |
1 |
The text stream has read past the end of the data in the underlying device. |
QTextStream::ReadCorruptData |
2 |
The text stream has read corrupt data. |
QTextStream::WriteFailed |
3 |
The text stream cannot write to the underlying device. |
FileIo::FileIo(QWidget *parent)
: QWidget(parent)
{
setWindowTitle("file system")
QFile file("E:/1.txt")
QTextStream out(&file)
QTextStream cout(stdout)
file.open(QFile::ReadWrite|QFile::Text)
if(out.atEnd())cout<<"this is end\n"
cout<<"pos:"<<out.pos()<<endl
out.setCodec("utf-8")
out.seek(0)
cout.setIntegerBase(10)
cout.setRealNumberPrecision(2)
cout<<out.readAll()<<endl
cout<<out.device()<<endl
cout<<bin<<5023.5/1024.0
file.close()
}