Qt 判斷文件或文件夾是否存在及創建文件夾

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()方法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章