Qt判断文件 或 文件夹(路径)是否存在
2017-04-22 14:46
483 查看
1. 判断文件夹是不是存在
参数说明:
QString fullPath;//文件夹全路径
/*方法1*/
bool isDirExist(QString fullPath)
{
QDir dir(fullPath);
if(dir.exists())
{
return true;
}
return false;
}
/*方法2*/
bool isDirExist(QString fullPath)
{
QFileInfo fileInfo(fullPath);
if(fileInfo.isDir())
{
return true;
}
return false;
}
2. 判断文件是不是存在
参数说明:
QString fullFileName;//文件全路径(包含文件名)
/*方法1*/
bool isFileExist(QString fullFileName)
{
QFileInfo fileInfo(fileFullName);
if(fileInfo.isFile())
{
return true;
}
return false;
}
3、判断文件或文件夹是不是存在(即不确定字符串是文件还是文件夹路径)
参数说明:
QString fullFilePath;//路径名
/*方法1*/
bool isFileExist(QString fullFilePath)
{
QFileInfo fileInfo(fullFilePath);
if(fileInfo.exists())
{
return true;
}
return false;
}
/*方法2*/
bool isFileExist(QString fullFilePath)
{
QFile file(fullFilePath);
if(file.exists())
{
return true;
}
return false;
}
4、判断文件夹是否存在,不存在则创建
/*方法1*/
bool isDirExist(QString fullPath)
{
QDir dir(fullPath);
if(dir.exists())
{
return true;
}
else
{
bool ok = dir.mkdir(fullPath);//只创建一级子目录,即必须保证上级目录存在
return ok;
}
}
/*方法1*/
bool isDirExist(QString fullPath)
{
QDir dir(fullPath);
if(dir.exists())
{
return true;
}
else
{
bool ok = dir.mkpath(fullPath);//创建多级目录
return ok;
}
}
5、以下为摘录的其他网络测试代码
{
QFileInfo fi("C:/123"); // 目录存在
qDebug() << fi.isFile(); // false
qDebug() << fi.isDir(); // true
qDebug() << fi.exists(); // true
qDebug() << fi.isRoot(); // false
qDebug() << QFile::exists("C:/123"); // true
qDebug() << QDir("C:/123").exists(); // true
fi.setFile("C:/ABC"); // 目录不存在
qDebug() << fi.isFile(); // false
qDebug() << fi.isDir(); // false
qDebug() << fi.exists(); // false
qDebug() << fi.isRoot(); // false
qDebug() << QFile::exists("C:/ABC"); // false
qDebug() << QDir("C:/ABC").exists(); // false
fi.setFile("C:/"); // 存在的驱动器
qDebug() << fi.isFile(); // false
qDebug() << fi.isDir(); // true
qDebug() << fi.exists(); // true
qDebug() << fi.isRoot(); // true
qDebug() << QFile::exists("C:/"); // true
qDebug() << QDir("C:/").exists(); // true
fi.setFile("Z:/"); // 不存在的驱动器
qDebug() << fi.isFile(); // false
qDebug() << fi.isDir(); // false
qDebug() << fi.exists(); // false
qDebug() << fi.isRoot(); // false
qDebug() << QFile::exists("Z:/"); // false
qDebug() << QDir("Z:/").exists(); // false
fi.setFile("C:/123.lnk"); // 快捷方式存在且指向的文件也存在
qDebug() << fi.isFile(); // true
qDebug() << fi.isDir(); // false
qDebug() << fi.exists(); // true
qDebug() << fi.isRoot(); // false
qDebug() << QFile::exists("C:/123.lnk"); // true
qDebug() << QDir("C:/123.lnk").exists(); // false
fi.setFile("C:/456.lnk"); // 快捷方式存在但指向的文件不存在
qDebug() << fi.isFile(); // false
qDebug() << fi.isDir(); // false
qDebug() << fi.exists(); // false
qDebug() << fi.isRoot(); // false
qDebug() << QFile::exists("C:/456.lnk"); // false
qDebug() << QDir("C:/456.lnk").exists(); // false
}
可以看到,容易让人感到混乱的是exists方法,这个方法是通用的判断方法,可以看成是这样的表达式
exists() == (isFile() || isDir())
也就是说判断文件或文件夹是否存在单纯用exists方法是不严谨的
比如你的本意是判断文件是否存在,但文件不存在,而恰巧有个同名的文件夹,那么exists也会返回true。文件夹也是同理
根据上面的代码作出的一点总结
准确判断文件是否存在
1.用QFileInfo::isFile()方法
准确判断文件夹是否存在
1.用QFileInfo::isDir()方法
2.用QDir::exists()方法
不确定字符串是文件还是文件夹路径
1.用QFileInfo::exists()方法
2.用QFile::exists()方法
参数说明:
QString fullPath;//文件夹全路径
/*方法1*/
bool isDirExist(QString fullPath)
{
QDir dir(fullPath);
if(dir.exists())
{
return true;
}
return false;
}
/*方法2*/
bool isDirExist(QString fullPath)
{
QFileInfo fileInfo(fullPath);
if(fileInfo.isDir())
{
return true;
}
return false;
}
2. 判断文件是不是存在
参数说明:
QString fullFileName;//文件全路径(包含文件名)
/*方法1*/
bool isFileExist(QString fullFileName)
{
QFileInfo fileInfo(fileFullName);
if(fileInfo.isFile())
{
return true;
}
return false;
}
3、判断文件或文件夹是不是存在(即不确定字符串是文件还是文件夹路径)
参数说明:
QString fullFilePath;//路径名
/*方法1*/
bool isFileExist(QString fullFilePath)
{
QFileInfo fileInfo(fullFilePath);
if(fileInfo.exists())
{
return true;
}
return false;
}
/*方法2*/
bool isFileExist(QString fullFilePath)
{
QFile file(fullFilePath);
if(file.exists())
{
return true;
}
return false;
}
4、判断文件夹是否存在,不存在则创建
/*方法1*/
bool isDirExist(QString fullPath)
{
QDir dir(fullPath);
if(dir.exists())
{
return true;
}
else
{
bool ok = dir.mkdir(fullPath);//只创建一级子目录,即必须保证上级目录存在
return ok;
}
}
/*方法1*/
bool isDirExist(QString fullPath)
{
QDir dir(fullPath);
if(dir.exists())
{
return true;
}
else
{
bool ok = dir.mkpath(fullPath);//创建多级目录
return ok;
}
}
5、以下为摘录的其他网络测试代码
{
QFileInfo fi("C:/123"); // 目录存在
qDebug() << fi.isFile(); // false
qDebug() << fi.isDir(); // true
qDebug() << fi.exists(); // true
qDebug() << fi.isRoot(); // false
qDebug() << QFile::exists("C:/123"); // true
qDebug() << QDir("C:/123").exists(); // true
fi.setFile("C:/ABC"); // 目录不存在
qDebug() << fi.isFile(); // false
qDebug() << fi.isDir(); // false
qDebug() << fi.exists(); // false
qDebug() << fi.isRoot(); // false
qDebug() << QFile::exists("C:/ABC"); // false
qDebug() << QDir("C:/ABC").exists(); // false
fi.setFile("C:/"); // 存在的驱动器
qDebug() << fi.isFile(); // false
qDebug() << fi.isDir(); // true
qDebug() << fi.exists(); // true
qDebug() << fi.isRoot(); // true
qDebug() << QFile::exists("C:/"); // true
qDebug() << QDir("C:/").exists(); // true
fi.setFile("Z:/"); // 不存在的驱动器
qDebug() << fi.isFile(); // false
qDebug() << fi.isDir(); // false
qDebug() << fi.exists(); // false
qDebug() << fi.isRoot(); // false
qDebug() << QFile::exists("Z:/"); // false
qDebug() << QDir("Z:/").exists(); // false
fi.setFile("C:/123.lnk"); // 快捷方式存在且指向的文件也存在
qDebug() << fi.isFile(); // true
qDebug() << fi.isDir(); // false
qDebug() << fi.exists(); // true
qDebug() << fi.isRoot(); // false
qDebug() << QFile::exists("C:/123.lnk"); // true
qDebug() << QDir("C:/123.lnk").exists(); // false
fi.setFile("C:/456.lnk"); // 快捷方式存在但指向的文件不存在
qDebug() << fi.isFile(); // false
qDebug() << fi.isDir(); // false
qDebug() << fi.exists(); // false
qDebug() << fi.isRoot(); // false
qDebug() << QFile::exists("C:/456.lnk"); // false
qDebug() << QDir("C:/456.lnk").exists(); // false
}
可以看到,容易让人感到混乱的是exists方法,这个方法是通用的判断方法,可以看成是这样的表达式
exists() == (isFile() || isDir())
也就是说判断文件或文件夹是否存在单纯用exists方法是不严谨的
比如你的本意是判断文件是否存在,但文件不存在,而恰巧有个同名的文件夹,那么exists也会返回true。文件夹也是同理
根据上面的代码作出的一点总结
准确判断文件是否存在
1.用QFileInfo::isFile()方法
准确判断文件夹是否存在
1.用QFileInfo::isDir()方法
2.用QDir::exists()方法
不确定字符串是文件还是文件夹路径
1.用QFileInfo::exists()方法
2.用QFile::exists()方法
相关文章推荐
- Qt - 正确判断文件、文件夹是否存在的方法
- Qt:正确判断文件、文件夹是否存在的方法
- asp.net 判断指定路径指定文件夹下是否有图片文件存在
- 【C#】C#判断文件路径是否存在,不存在则创建文件夹
- Qt 判断文件或文件夹是否存在及创建文件夹
- 【Qt】正确判断文件、文件夹是否存在的方法
- 用DirectoryExists和FileExists判断指定路径下是否存在指定的文件夹或文件
- 【Qt】正确判断文件、文件夹是否存在的方法
- MFC判断指定路径下目标文件、目标文件夹是否存在
- python里面判断文件夹或者文件的路径是否存在
- Ant 判断某个变量是否被赋值,某个文件|路径(文件夹)是否存在
- 【Qt】正确判断文件、文件夹是否存在的方法
- 在指定路径创建文件夹,读取图片文件,缩放图片,判断文件夹是否存在,
- 【路径相关方法】3,判断某路径的文件和文件夹是否存在
- Qt - 正确判断文件、文件夹是否存在的方法
- python 判断路径是文件还是文件夹、判断是否存在、获取文件大小
- c++ 判断文件、文件夹(路径)是否存在
- 您现在的位置:首页 > 技术博客 > 正文 Qt - 正确判断文件、文件夹是否存在的方法
- 用_access判断文件或文件夹是否存在及其读写权限
- 几个C#编程的小技巧 (二) (判断文件或文件夹是否存在 )