【QT】QT中的文件IO

一、有关文件IO的类

QFile 表示某个文件对象
QDir 表示某个目录对象
QFileInfo 存放文件的属性信息(文件大小,文件类型等等)

二、步骤

1、定义QFile的对象,与要读写的文件绑定在一起

QFile::QFile(const QString &name)
参数:name --》需要读写的文件路径名

2、打开文件

bool QIODevice::open(OpenMode mode)
	返回值:成功 true  失败 false
	参数:QIODevice::ReadOnly    //只读
		QIODevice::WriteOnly   //只写
		QIODevice::ReadWrite   //只读写
新建文件  open(QIODevice::WriteOnly|QIODevice::Truncate)

3、读写文件

1)读取文件

qint64 QIODevice::read(char *data, qint64 maxSize)
	返回值:成功 返回读取的字节数  失败 -1
	参数:data --》保存读取的数据
		maxSize --》读取多少字节的数据
		
QByteArray QIODevice::read(qint64 maxSize)
	返回值:读取的数据
	参数:maxSize --》读取多少字节的数据
QByteArray QIODevice::readAll()  //一次性读取整个文件内容,适合读取小文件
qint64 QIODevice::readLine(char *data, qint64 maxSize) //一次最多读取一行文件数据
类比:fgets

2)写入文件

qint64 QIODevice::write(const char *data, qint64 maxSize)
qint64 QIODevice::write(const QByteArray &byteArray)

4、关闭文件

void QIODevice::close()

5、示例代码:

// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDebug>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    ui->textBrowser->setText("你好,世界aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}

MainWindow::~MainWindow()
{
    delete ui;
}

// pushbutton 右键转到槽
void MainWindow::on_pushButton_clicked()
{
    QString pathName = QFileDialog::getOpenFileName(this);
    // 创建文件对象
    QFile myFile(pathName);
    // 打开文件
    bool ret = myFile.open(QIODevice::ReadWrite);
    if (!ret)
    {
        QMessageBox::warning(this, "警告", "文件打开失败");
        return;
    }

    // 读取文件内容
    // char buf[100]={0};
    // myfile.read(buf,100); //读取100个字节存放到buf中
    // qDebug()<<"读取的内容: "<<buf;

    // QByteArray buf=myfile.read(100);  //读取100个字节,以返回值的形式返回
    // QByteArray叫做字节数组,里面的内容是16进制的形式显示
    // 把QByteArray转换成QString  QString(const QByteArray &ba)
    // QString str(buf);
    // qDebug()<<"读取的内容: "<<str;

    // 一次性读取整个文件(视频几十M以上的无法读取)
    QByteArray buf=myFile.readAll();
    //在文本浏览框中显示
    ui->textBrowser->setText(buf);

    //关闭文件
    myFile.close();
}

三、QString和QByteArray之间的转换

1、方法

QByteArray --》QString   自动转换,调用了构造函数
QString --》QByteArray   必须使用函数转换  QByteArray QString::toUtf8()
char * --》QString       自动转换,调用了构造函数
QString --char *       调用QString中的data()方法

2、示例代码:

// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QByteArray>
#include <QString>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 1、QByteArray --》QString   自动转换(调用了构造函数)
    QByteArray byteArry("你好,世界");
    QString str1 = byteArry;  // 跟QString str1(byteArry)等价的
    qDebug()<<"str1:"<<str1;  // str1: "你好,世界"

    // 2、QString --》QByteArray   必须使用函数转换  QByteArray QString::toUtf8()
    QString str2="helloworld";
    QByteArray array2=str2.toUtf8();
    qDebug()<<"array2:"<<array2;  // array2: "helloworld"

    // 3、char * --》QString       自动转换(调用了构造函数)
    char buf[10]="你好";
    QString str3=buf;  //跟QString str3(buf)等价的
    qDebug()<<"str3:"<<str3;  // str3: "你好"

    // 4、QString --》char *       调用QString中的data()方法
    QString str4="nihao";
    QChar *p=str4.data();
    qDebug() << "通过循环逐个输出字符:";
    for (int i = 0; p[i] != '\0'; ++i)
    {
        qDebug() << p[i];
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

四、QFileInfo类 --》获取文件属性

1、方法

QFileInfo::QFileInfo(const QString &file)
	参数:file --》文件的路径名
QString QFileInfo::filePath() const  //获取文件的路径名
bool QFileInfo::isDir() const        //判断文件是否是目录
qint64 QFileInfo::size() const       //获取文件大小
QString QFileInfo::suffix() const    //获取文件的后缀名

五、QDir类 --》QT中的目录操作

1、方法

第一步:新建QDir的对象,跟某个目录的路径绑定一起
QDir::QDir(const QString &path = QString())
	参数:path --》要遍历的目录的路径名
第二步:遍历目录,得到目录的文件名
QStringList QDir::entryList() const
	返回值:字符串列表中存放了目录中所有的文件名字

2、示例代码(与QFileInfo配合使用)

// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDir>
#include <QDebug>
#include <QFileInfo>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

// 打开目录
void MainWindow::on_pushButton_clicked()
{
    // 打开目录对话框
    QString dirpath=QFileDialog::getExistingDirectory(this,"我的目录");
    //qDebug()<<"选择的目录是: "<<dirpath;

    // 创建QDir的对象表示某个目录
    QDir mydir(dirpath);

    // 遍历目录
    QStringList filelist=mydir.entryList();
    for(auto x:filelist)
        qDebug()<<"选择的目录中包含了: "<<x;

    // 获取文件的属性

    QFileInfo info("C:/Users/Administrator/Desktop/vm_share/笔记/1.txt");
    // 获取文件大小
    qDebug()<<"文件大小是: "<<info.size();
    qDebug()<<"文件后缀名是: "<<info.suffix();

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值