【接口封装】——22、读写文件
解释:
1、封装内容:对不同文件内容的读取->处理->写入
2、文件流写入
3、插入处理函数: if (!replaceJsTxt(tmpFileContent)) return;
函数定义:
#include <QMessageBox> #include <QTextStream> void initJsFile();
源代码:
void TalkWindowShell::initJsFile() { //读取 QFile tmpFile(QRC_msgPageTmp_txt); QString tmpFileContent; //资源文件(.qrc)只能读不能直接写 if (tmpFile.open(QFile::ReadOnly)) { tmpFileContent = tmpFile.readAll(); //获取内容 tmpFile.close(); } else { QMessageBox::information(nullptr, "error", "Read QRC_msgPageTmp_txt Error."); return; } //处理函数!!!replaceJsTxt() if (!replaceJsTxt(tmpFileContent)) return; //写入——替换后的内容 QFile js(PATH_msghandling_js);//当前目录 if (js.open(QFile::WriteOnly | QFile::Truncate)) {//有内容则清空 QTextStream stream(&js); stream << tmpFileContent; //文件流写入 js.close(); return; } else { QMessageBox::information(nullptr, "error", "Write PATH_msghandling_js Error."); return; } }