void file_info()
{
QFileInfo info = QFileInfo("filePath");
QString file_name = info.fileName();
QString file_suffix = info.suffix()
QString path = info.absolutePath();
int size = info.size();
}
static QString readFile(const QString &filePath) {
QString line = "";
QFile file(filePath);
if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
while (!in.atEnd()) {
line = in.readLine();
}
file.close();
}
return line;
}
static void writeFile(const QString &filePath, const QString &str) {
QFile file(filePath);
if(file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << str << "\n";
file.close();
}
}
static QString readFile2(const QString &filePath) {
QString line = "";
QFile file(filePath);
if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
file.readAll();
file.close();
}
return line;
}
static void writeFile2(const QString &filePath, const QString &str | QIODevice::Append) {
QFile file(filePath);
if(file.open(QIODevice::WriteOnly | QIODevice::Text)) {
file.write(str.toUtf8());
file.close();
}
}
static bool is_file_exits(const QString &filePath){
QFileInfo fileInfo(filePath);
return fileInfo.isFile();
}
static void create_file(const QString &filePath) {
if(is_file_exits(filePath)) {
QFile file(filePath);
file.open(QIODevice::WriteOnly);
file.close();
}
}
static void remove_file(const QString &filePath) {
if(is_file_exits(filePath)) {
QFile::remove(filePath);
}
}
static void create_dir(QString &dirPath) {
if(dirPath.endsWith("/")){
dirPath.chop(1);
}
QDir dir(dirPath);
if(!dir.exists()) {
dir.mkpath(dirPath);
}
}
static void clear_dir(const QString &dirPath) {
QDir dir(dirPath);
dir.setFilter(QDir::Files);
int fileCount = dir.count();
for (int i = 0; i < fileCount; i++) {
dir.remove(dir[i]);
}
}
void find_info()
{
QDir dir;
QStringList filters;
filters << "*.jpg"
<< "*.jpeg"
<< "*.png";
dir.setNameFilters(filters);
QFileInfoList li = dir.entryInfoList();
for (int i = 0; i < li.size(); ++i)
{
qDebug() << li.at(i).fileName();
}
}
读文件
一次性读完
QByteArray data = file.readAll();
一次读一行
QByteArray data;
while(file.atEnd == false) {
data += readLine();
}