QFileInfo类提供与系统无关的文件信息。
判断文件夹是否存在或者是不是文件夹
bool isExistDir(QString fullPath)
{
QFileInfo fileInfo(fullPath);
if(fileInfo.isDir())
{
return true;
}
return false;
}
1.判断文件是否存在是不是或者是不是文件
bool isExistFile(QString fullPath)
{
QFileInfo fileInfo(fullPath);
if(fileInfo.isFile())
{
return true;
}
return false;
}
判断是否存在文件或者文件夹
bool isExist(QString fullPath)
{
QFileInfo fileInfo(fullPath);
if(fileInfo.exists())
{
return true;
}
return false;
}
获取文件的名称(不包含路径名和后缀)
int getFileBaseName(QString fullPath, QString &fileBaseName)
{
QFileInfo fileInfo(fullPath);
if(!fileInfo.exists())
{
fileBaseName = "null";
return 0;
}
fileBaseName = fileInfo.baseName();
return 1;
}
获取文件的名称(不包含路径名,包含后缀)
int getFileName(QString fullPath, QString &fileName)
{
QFileInfo fileInfo(fullPath);
if(!fileInfo.exists())
{
fileName = "null";
return 0;
}
fileName = fileInfo.fileName();
return 1;
}
获取文件的绝对路径(包含文件名)
int getAbsoluteFilePath(QString fullPath, QString &absoluteFilePath)
{
QFileInfo fileInfo(fullPath);
if(!fileInfo.exists())
{
absoluteFilePath = "null";
return 0;
}
absoluteFilePath = fileInfo.absoluteFilePath();
return 1;
}
获取文件的绝对路径(不包含文件名)
int getAbsolutePath(QString fullPath, QString &absolutePath)
{
QFileInfo fileInfo(fullPath);
if(!fileInfo.exists())
{
absolutePath = "null";
return 0;
}
absolutePath = fileInfo.absolutePath();
return 1;
}
获取文件的创建时间
int getFileTime(QString fullPath, QDateTime &fileTime)
{
QFileInfo fileInfo(fullPath);
if(!fileInfo.exists())
{
return 0;
}
fileTime = fileInfo.birthTime();
return 1;
}
测试
文件存在
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString filePath= "D:/QtProject/QFileInfo_Example/QFileInfo_Example.pro";
qDebug()<<"isExistDir:"<<isExistDir(filePath);
qDebug()<<"isExistDir:"<<isExistFile(filePath);
qDebug()<<"isExist:"<<isExist(filePath);
QString fileBaseName;
getFileBaseName(filePath,fileBaseName);
qDebug()<<"getFileBaseName:"<<fileBaseName;
QString filName;
getFileName(filePath,filName);
qDebug()<<"getFileName:"<<filName;
QString absoluteFilePath;
getAbsoluteFilePath(filePath,absoluteFilePath);
qDebug()<<"getAbsoluteFilePath:"<<absoluteFilePath;
QString absolutePath;
getAbsolutePath(filePath,absolutePath);
qDebug()<<"getAbsoluteFilePath:"<<absolutePath;
QDateTime fileTime;
getFileTime(filePath,fileTime);
qDebug()<<"getFileTime:"<<fileTime;
return a.exec();
}
打印输出
文件不存在
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString filePath= "C:/QtProject/QFileInfo_Example/QFileInfo_Example.pro";
qDebug()<<"isExistDir:"<<isExistDir(filePath);
qDebug()<<"isExistDir:"<<isExistFile(filePath);
qDebug()<<"isExist:"<<isExist(filePath);
QString fileBaseName;
getFileBaseName(filePath,fileBaseName);
qDebug()<<"getFileBaseName:"<<fileBaseName;
QString filName;
getFileName(filePath,filName);
qDebug()<<"getFileName:"<<filName;
QString absoluteFilePath;
getAbsoluteFilePath(filePath,absoluteFilePath);
qDebug()<<"getAbsoluteFilePath:"<<absoluteFilePath;
QString absolutePath;
getAbsolutePath(filePath,absolutePath);
qDebug()<<"getAbsoluteFilePath:"<<absolutePath;
QDateTime fileTime;
getFileTime(filePath,fileTime);
qDebug()<<"getFileTime:"<<fileTime;
return a.exec();
}
打印输出
|