Qt沒有提供單獨的函數來對文件、文件夾進行操作, 但是提供了兩個類: QFile, QDir;
1、文件操作
1) 文件是否存在; QFile file("D:/test.jpg"); if( !file.exists() ) { ... } 2) 文件復制; [static] bool QFile::copy(const QString &fileName, const QString &newName) 文件復制函數為靜態函數; 3) 文件刪除; QFile file("D:/test.jpg"); if(!file.remove()) { ... //失敗; }else{ ... //成功; } 4) 文件讀寫; QFile一般是配合QDataStream或QTextStream進行操作, 或者利用繼承QIODevice的read(), write() , readAll(), readLine()進行操作;
2、文件夾操作;
1)判斷文件夾是否存在,並進行創建; QDir dir; if( ! dir.exists( szPath) ) { dir.mkpath(szPath); } 2) 文件夾刪除; QDir提供了兩種刪除文件夾的方法: rmdir 和 rmpath; 這兩者的區別在於: rmdir只能刪除空目錄, rmpath能將path的父目錄一下的路徑全部刪除;