介紹
兩者都是在構造時建立一個随機名稱的目錄或檔案,并在其銷毀時自動删除對應的目錄和檔案,同時兩者均能保證不會覆寫已有檔案。
執行個體化時若不傳遞參數則随機确定名稱,若傳入名稱會優先嘗試指定名稱若此名稱已存在檔案則會随機建立。
file的父類是QFile,可以進行QFile的所有操作。
但要注意Dir的父類隻是QObject并不是QDir,也很正常,畢竟隻是個臨時檔案夾不需要其他操作。
QTemporaryDir
接口說明
QTemporaryDir()
QTemporaryDir(const QString &templatePath)
~QTemporaryDir()
bool autoRemove() const
QString errorString() const
QString filePath(const QString &fileName) const
bool isValid() const
QString path() const
bool remove()
void setAutoRemove(bool b)
注意構造函數說明,支援相對路徑:
If templatePath is a relative path, the path will be relative to the current working directory.
同時注意對于路徑末尾字元的說明,檔案夾路徑會直接在指定路徑之後添加随機字元,如果指定路徑最後不是/則為指定名稱+XXX構成新路徑名,如果最後是/則在前面的路徑後建立一個随機目錄,注意此時必須保證前面的檔案夾都存在否則出錯,見後面的範例:
If the templatePath ends with XXXXXX it will be used as the dynamic portion of the directory name, otherwise it will be appended. Unlike QTemporaryFile, XXXXXX in the middle of the template string is not supported.
remove可以主動提前删除目錄,注意會删除目錄下所有檔案,畢竟是臨時目錄不要存有用的東西
Removes the temporary directory, including all its contents.
autoremove預設是true
最後,filePath可以擷取檔案的路徑名,這個比較特殊,需要傳入一個檔案名,此函數傳回一個完整的路徑名。這樣可以用于後續的QTemporaryFile。
範例
#include <QCoreApplication>
#include <QDebug>
#include <QTemporaryFile>
#include <QTemporaryDir>
int main(int argc, char *argv[]) {
QCoreApplication a(argc,argv);
QTemporaryDir testdir1;
qDebug()<<testdir1.autoRemove();
qDebug()<<testdir1.filePath("123.txt");
QTemporaryDir testdir2("testdir2");
qDebug()<<testdir2.autoRemove();
qDebug()<<testdir2.filePath("123.txt");
QTemporaryDir testdir3("testdir3/");
//注意這樣等于是指定在目前運作目錄下的testdir3目錄下建立一個随機名稱的目錄
//如果testdir3檔案夾不存在将會出錯
qDebug()<<testdir3.autoRemove();
qDebug()<<testdir3.filePath("123.txt");
return 0;
}
結果
true
"C:/Users/XXXXXX/AppData/Local/Temp/untitled-GN2aKw/123.txt"
true
"testdir2bE7tFd/123.txt"
true
"testdir3/zxPK5t/123.txt"
XXXXXX是目前系統登入的使用者名,也就是預設目錄自動指向了系統預設的臨時檔案位址。
最後的testdir3目錄下建立臨時目錄,最後删除的隻是臨時目錄及其下所有檔案,并不會删除testdir3檔案夾
QTemporaryFile
接口說明
QTemporaryFile()
QTemporaryFile(const QString &templateName)
QTemporaryFile(QObject *parent)
QTemporaryFile(const QString &templateName, QObject *parent)
~QTemporaryFile()
bool autoRemove() const
QString fileTemplate() const
bool open()
void setAutoRemove(bool b)
void setFileTemplate(const QString &name)
構造函數可以傳入一個臨時檔案名,這個名稱就可以用QTemporaryDir::filePath實作在臨時目錄建立一個指定檔案名的臨時檔案。(如果這個檔案已經存在那麼還是會建立一個随機名稱的)
fileTemplate是檔案名,臨時檔案的實作原理是檔案名後面加上一個”.XXX”随機名稱,是以前面的檔案名可以随機指定
範例
#include <QCoreApplication>
#include <QDebug>
#include <QTemporaryFile>
#include <QTemporaryDir>
int main(int argc, char *argv[]) {
QCoreApplication a(argc,argv);
QTemporaryFile testfile1;//建立第一個檔案
qDebug()<<"testfile1"<<testfile1.fileName()
<<testfile1.fileTemplate();
//第一次open之前檔案是沒有建立的,是以沒名字
testfile1.open();
testfile1.close();
qDebug()<<"testfile1"<<testfile1.fileName()
<<testfile1.fileTemplate();
//隻要對象不被銷毀,可以重複open,不會變檔案
testfile1.open();
qDebug()<<"testfile1"<<testfile1.fileName()
<<testfile1.fileTemplate();
//指定名字
QTemporaryFile testfile2("testfile2");
testfile2.open();
qDebug()<<"testfile2"<<testfile2.fileName()
<<testfile2.fileTemplate();
//建立一個重名的
QTemporaryFile testfile3("testfile2");
testfile3.open();
qDebug()<<"testfile3"<<testfile3.fileName()
<<testfile3.fileTemplate();
//在QTemporaryDir臨時目錄下建立一個
QTemporaryDir testdir1;
qDebug()<<"testdir1"<<testdir1.filePath("testfile4.txt");
QTemporaryFile testfile4(testdir1.filePath("testfile4.txt"));
testfile4.open();
qDebug()<<"testfile4"<<testfile4.fileName()
<<testfile4.fileTemplate();
//注意最後一個就算檔案名定義了txt字尾,夜壺自動在後面加.xxxxxx
//QTemporaryFile繼承了QFile,在open以後可以直接進行QFile的所有操作
return 0;
}
結果
testfile1 "" "C:/Users/XXXXX/AppData/Local/Temp/untitled.XXXXXX"
testfile1 "C:/Users/XXXXX/AppData/Local/Temp/untitled.Hp9316" "C:/Users/zhouliang/AppData/Local/Temp/untitled.XXXXXX"
testfile1 "C:/Users/XXXXX/AppData/Local/Temp/untitled.Hp9316" "C:/Users/zhouliang/AppData/Local/Temp/untitled.XXXXXX"
testfile2 "D:/my_program_design/untitled/build-untitled-Desktop_Qt_5_9_2_MinGW_32bit-Debug/testfile2.gq9316" "testfile2"
testfile3 "D:/my_program_design/untitled/build-untitled-Desktop_Qt_5_9_2_MinGW_32bit-Debug/testfile2.Uh9316" "testfile2"
testdir1 "C:/Users/XXXXX/AppData/Local/Temp/untitled-i9GN2a/testfile4.txt"
testfile4 "C:/Users/XXXXX/AppData/Local/Temp/untitled-i9GN2a/testfile4.txt.lY9316" "C:/Users/zhouliang/AppData/Local/Temp/untitled-i9GN2a/testfile4.txt"
file的父類是QFile,可以進行QFile的所有操作。上述範例沒有示範讀寫操作。