Qt判断文件夹路径、文件是否存在不存在则创建

Qt判断文件夹路径、文件是否存在不存在则创建


Chapter1 Qt判断文件夹路径、文件是否存在不存在则创建

原文链接:https://blog.csdn.net/yao_hou/article/details/121389476

Qt判断文件夹/目录是否存在

Qt判断文件夹/目录是否存在,可以使用QDir类的exists方法来判断,在使用时需要包含头文件#include <QDir>,例如下面的代码:

cpp 复制代码
#include <QCoreApplication>
#include <QDir>
#include <QDebug>

///
/// \brief 判断文件夹是否存在,不存在则创建
/// \param fullPath
/// \return
///
bool DirExist(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.exists())
    {
        //存在当前文件夹
        return true;
    }
    else
    {
        //不存在则创建
        bool ok = dir.mkdir(fullPath); //只创建一级子目录,即必须保证上级目录存在
        return ok;
    }
}

///
/// \brief 判断文件夹是否存在,不存在则创建, 可创建多级目录
/// \param fullPath
/// \return
///
bool DirExistEx(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.exists())
    {
        return true;
    }
    else
    {
        //不存在当前目录,创建,可创建多级目录
        bool ok = dir.mkpath(fullPath);
        return ok;
    }
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    bool isOk = DirExist("D:/1234");
    qDebug() << isOk;

    isOk = DirExistEx("D:/1/2/3");
    qDebug() << isOk;

    return a.exec();
}
  • DirExist函数,判断文件夹是否存在,不存在则创建
  • DirExistEx函数,判断文件夹是否存在,不存在则创建, 可创建多级目录

二者的区别是创建文件夹调用的方法不同

  • QDir mkdir :创建一个目录(文件夹/路径)
  • QDir mkpath : 创建多级目录

Qt判断文件是否存在

代码如下:

cpp 复制代码
#include <QCoreApplication>
#include <QFile>
#include <QDebug>

///
/// \brief 判断文件是否存在,不存在则创建该文件
/// \param fullFileName
/// \return
///
bool FileExist(QString fullFileName)
{
    QFile file(fullFileName);

    if(file.exists())
    {
        return true;
    }
    else
    {
        qDebug() << u8"文件不存在, 那就新建该文件";
        file.open( QIODevice::ReadWrite | QIODevice::Text );
		
		//注意关闭文件
        file.close();
    }

    return false;
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    bool isOk = FileExist("D:/1234567.ini");  //Text类型可以创建txt,ini,json,xml等
    qDebug() << isOk;

    return a.exec();
}

当文件不存在时,调用QFile的open方法也就是相当于创建了一个文件。

Chapter2 Qt 判断文件或文件夹是否存在及创建文件夹

原文链接:https://blog.csdn.net/lusirking/article/details/51644782

1. 判断文件夹是不是存在

参数说明:

cpp 复制代码
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. 判断文件是不是存在

参数说明:

cpp 复制代码
QString fullFileName;//文件全路径(包含文件名)
/*方法1*/
bool isFileExist(QString fullFileName)
{
    QFileInfo fileInfo(fileFullName);
    if(fileInfo.isFile())
    {
        return true;
    }
    return false;
}

3、判断文件或文件夹是不是存在(即不确定字符串是文件还是文件夹路径)

参数说明:

cpp 复制代码
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、判断文件夹是否存在,不存在则创建

cpp 复制代码
/*方法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、以下为摘录的其他网络测试代码

cpp 复制代码
{
    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()方法

相关推荐
七七&5565 小时前
2024年08月13日 Go生态洞察:Go 1.23 发布与全面深度解读
开发语言·网络·golang
java坤坤6 小时前
GoLand 项目从 0 到 1:第八天 ——GORM 命名策略陷阱与 Go 项目启动慢问题攻坚
开发语言·后端·golang
元清加油6 小时前
【Golang】:函数和包
服务器·开发语言·网络·后端·网络协议·golang
健康平安的活着6 小时前
java之 junit4单元测试Mockito的使用
java·开发语言·单元测试
DjangoJason8 小时前
C++ 仿RabbitMQ实现消息队列项目
开发语言·c++·rabbitmq
m0_480502648 小时前
Rust 入门 KV存储HashMap (十七)
java·开发语言·rust
大阳1238 小时前
线程(基本概念和相关命令)
开发语言·数据结构·经验分享·算法·线程·学习经验
YA3338 小时前
java基础(九)sql基础及索引
java·开发语言·sql
奇树谦9 小时前
QT|windwos桌面端应用程序开发,当连接多个显示器的时候,如何获取屏幕编号?
开发语言·qt
weixin_307779139 小时前
VS Code配置MinGW64编译GNU 科学库 (GSL)
开发语言·c++·vscode·算法