QFile文件操作
文件打开方式:
QIODevice::NotOpen 0x0000 设备不打开.
QIODevice::ReadOnly 0x0001 设备 以只读的方式打开.
QIODevice::WriteOnly 0x0002 设备 以只写的方式打开.
QIODevice::ReadWrite ReadOnly | WriteOnly 设备以读写的方式打开,写入文件会覆盖之前的内容(打开文件期间多次写入不会覆盖).
QIODevice::Append 0x0004 设备以追加模式打开,以便将所有数据写入文件末尾,此模式下不能读文件.
QIODevice::Truncate 0x0008 如果可能,设备在打开之前会被截断。 设备的所有早期内容都将丢失。
QIODevice::Text 0x0010 读取时,行尾终止符被转换为’\ n’。 写入时,行尾终止符将转换为本地编码,例如Win32的“\ r \ n”。
QIODevice::Unbuffered 0x0020 绕过设备中的任何缓冲区。
新建文件夹
//创建temp-path文件夹
QString dir_str ="D:/test2/";
// 检查目录是否存在,若不存在则新建
QDir dir;
if(!dir.exists(dir_str))
{
bool res = dir.mkpath(dir_str);
}
QFile temp_path("D:/test2/temp.txt");
//如果txt文件路径不存在
if(!dir.exists("D:/test2/temp.txt"))
temp_path.open(QIODevice::WriteOnly);//创建txt
创建文件
QString filename = "fileName.zip";
QString file = "D:/test/" + filename;
QFile *myfile=new QFile(this);
myfile->setFileName(file);
bool ret = myfile->open(QIODevice::WriteOnly|QIODevice::Truncate); //创建文件
if(!ret)
{
QMessageBox::warning(this,"warning","打开失败");
return;
}
参考:1.Qt文件系统之QFile