[Qt] Qt文件操作 [2013-08-13更新]

来源:互联网 发布:周末算法定假日吗 编辑:程序博客网 时间:2024/06/13 23:24
- 经验总结

1. 直接访问模式在输入/输出的块操作使用4千字节或更大的情况下使用最好。

2. 当每次读小量数据时,缓存访问工作的更好。

3. 重要:当使用缓存的文件工作时,数据没有被立刻写到文件中。调用flush()可以确保数据被真正地写完。 

4. 警告:如果你有一个缓存文件以同时读写方式打开,你不要在一次输入操作之后立即输出,反之亦然。你必须在输入和输出操作之间调用flush()或者一次文件定位操作,比如seek(),否则缓存中可能会包含无用信息。 

5. 如果文件不存在并且指定IO_WriteOnly或IO_ReadWrite时,文件将被创建。



- 基本操作

#include <QFile>
#include <QTextStream>

QFile file("/../filename");

- 读文件

功能:把文件的内容读取到fileContent

if ( file.open(QIODevice::ReadOnly ) )
{
    QTextStream stream( &file );
    QString fileContent;
    QString line;
    while ( !stream.atEnd() )
    {
        line = stream.readLine();           // 不包括“n”的一行文本
        fileContent.append(line).append("\n");
    }
    file.close();
}

QStringList lines;
if ( file.open(QIODevice::ReadOnly ) )
{
    QTextStream stream( &file );
    QString line;
    int n = 1;
    while ( !stream.atEnd() )
    {
        line = stream.readLine();      // 不包括“n”的一行文本
        //printf( "%3d: %sn", n++, line.toLatin1() );
        lines += line;
    }
    file.close();
}

-------------------------------------------------------------------------

- 写文件

功能:将strtmp的内容写入文件

if ( file.open( IO_WriteOnly ) )  // QIODevice::WriteOnly
{
    QTextStream stream( &file );
    stream << strtmp << "\n";
    file.close();
}

QStringList lines;
if ( file.open( IO_WriteOnly ) )
{
    QTextStream stream( &file );
    for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
    {
        stream << *it << "\n";
    }
    file.close();
}

-------------------------------------------------------------------------

- 删除文件

QFile::remove("/.../fileName");



- 文件打开方式

#define IO_ReadOnly      QIODevice::ReadOnly
#define IO_WriteOnly     QIODevice::WriteOnly
#define IO_ReadWrite     QIODevice::ReadWrite
#define IO_Append        QIODevice::Append
#define IO_Truncate      QIODevice::Truncate
#define IO_Translate     QIODevice::Text
#define IO_ModeMask      0x00FF

IO_Raw                   指定直接的(非缓存的)文件访问
IO_ReadOnly              以只读模式打开文件
IO_WriteOnly             以只写模式(并且截短)打开文件
IO_ReadWrite             以读/写模式打开文件,等于(IO_ReadOnly | IO_WriteOnly)
IO_Append                以附加的模式打开文件(当你想向日志文件写些东西时这个模式非常有用)
                         文件索引被设置到文件末尾,使用while循环遍历文件时需重置文件索引
                         注意如果你在附加模式中使用at()定位文件索引,结果将为定义的
IO_Truncate              截短文件
IO_Translate             在MS-DOS、Windows和OS/2下对文本文件翻译回车和换行



- 判断文件或目录是否存在

#include <QFileInfo>

QFile file("...");
QFileInfo fileInfo(file);
or
QFileInfo fileInfo;
fileInfo.setFile(filePath);

bool QFileInfo::exists () const
Returns true if the file exists; otherwise returns false.

bool QFileInfo::isDir () const
Returns true if this object points to a directory or to a symbolic link to a directory; otherwise returns false.



- 打开指定的文件夹

QDesktopServices::openUrl(QUrl("file:///C:/Documents and Settings/All Users/"));



- 遍历某个目录下的文件

#include <QDir>

QDir dir;
dir.cd("/directory");                 // 指定目录
dir.setFilter(QDir::Files);           // 过滤器
dir.setSorting(QDir::Name);           // 排序依据(此处为按名称排序)

QFileInfoList list = dir.entryInfoList();

for (int i = 0; i < list.size(); ++i)
{
    QFileInfo fileInfo = list.at(i);
    fileInfo.fileName()               // 获取文件名
    fileInfo.size()                   // 获取文件大小
}


原创粉丝点击