1. 程式人生 > 其它 >qt 判斷檔案是否存在

qt 判斷檔案是否存在

1. 判斷資料夾是不是存在

 1 QString fullPath;//資料夾全路徑
 2 /*方法1*/
 3 bool isDirExist(QString fullPath)
 4 {
 5     QDir dir(fullPath);
 6     if(dir.exists())
 7     {
 8       return true;
 9     }
10     return false;
11 }
12 /*方法2*/
13 bool isDirExist(QString fullPath)
14 {
15     QFileInfo fileInfo(fullPath);
16
if(fileInfo.isDir()) 17 { 18 return true; 19 } 20 return false; 21 }
View Code

2. 判斷檔案是不是存在

 1 QString fullFileName;//檔案全路徑(包含檔名)
 2 /*方法1*/
 3 bool isFileExist(QString fullFileName)
 4 {
 5     QFileInfo fileInfo(fileFullName);
 6     if(fileInfo.isFile())
 7     {
 8         return
true; 9 } 10 return false; 11 }
View Code

3、判斷檔案或資料夾是不是存在(即不確定字串是檔案還是資料夾路徑)

 1 QString fullFilePath;//路徑名
 2 /*方法1*/
 3 bool isFileExist(QString fullFilePath)
 4 {
 5     QFileInfo fileInfo(fullFilePath);
 6     if(fileInfo.exists())
 7     {
 8         return true;
 9     }
10     return false
; 11 } 12 /*方法2*/ 13 bool isFileExist(QString fullFilePath) 14 { 15 QFile file(fullFilePath); 16 if(file.exists()) 17 { 18 return true; 19 } 20 return false; 21 }
View Code

4、判斷資料夾是否存在,不存在則建立

 1 /*方法1*/
 2 bool isDirExist(QString fullPath)
 3 {
 4     QDir dir(fullPath);
 5     if(dir.exists())
 6     {
 7       return true;
 8     }
 9     else
10     {
11        bool ok = dir.mkdir(fullPath);//只建立一級子目錄,即必須保證上級目錄存在
12        return ok;
13     }
14 }
15 
16 /*方法2*/
17 bool isDirExist(QString fullPath)
18 {
19     QDir dir(fullPath);
20     if(dir.exists())
21     {
22       return true;
23     }
24     else
25     {
26        QDir dir1;
27        bool ok = dir1.mkpath(fullPath);//建立多級目錄
28        return ok;
29     }
30 }
View Code

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
}
View Code

可以看到,容易讓人感到混亂的是exists方法,這個方法是通用的判斷方法,可以看成是這樣的表示式
exists() == (isFile() || isDir())

也就是說判斷檔案或資料夾是否存在單純用exists方法是不嚴謹的
比如你的本意是判斷檔案是否存在,但檔案不存在,而恰巧有個同名的資料夾,那麼exists也會返回true。資料夾也是同理

根據上面的程式碼作出的一點總結

準確判斷檔案是否存在
1.用QFileInfo::isFile()方法

準確判斷資料夾是否存在
1.用QFileInfo::isDir()方法
2.用QDir::exists()方法

不確定字串是檔案還是資料夾路徑
1.用QFileInfo::exists()方法
2.用QFile::exists()方法