0
点赞
收藏
分享

微信扫一扫

QIODevice类的使用方法

  1. QIODevice是Qt所有I/O设备的基础接口类
  2. 为QFile、QBuffer和QTcpSocket等支持读/写数据块设备提供了抽象类
  3. QIODevice类是抽象类,无法被实例化
  4. 访问一个设备前,需要使用打开函数open()来打开设备,而且必须要指定正确的打开模式。打开设备后可以使用write()或者putchar()来进行写入,使用read()、readAll()进行读取,最后使用close()关闭设备
  5. QIODevice的打开模式

QIODevice类的使用方法_打开文件

#include <QDateTime>
#include <QDebug>

int main(int argc, char *argv[])
{
QFile file("test.txt");
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << file.errorString();
}
file.write("QIODevice! \myproject");
file.close();

QFileInfo info(file);
qDebug <<QObject::tr("绝对路径:")<<info.absoluteFilePath()<<endl;
<<QObject::tr("文件名:")<<info.fileName()<<endl;
<<QObject::tr("基本名称")<<info.baseName()<<endl;
<<QObject::tr("创建时间")<<info.created()<<endl;
<<QObject::tr("大小")<<info.size();
}

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>//文件读写
#include<QDir>//目录
#include<QTextStream>//文件流
#include<QFileDialog>//文件对话框
#include<QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
//打开文件
void MainWindow::on_openfile_clicked()
{
QString curPath =QDir::currentPath();//获取当前路径
qDebug()<<"curPath="<<curPath<<endl;//默认编译路径
QString openTitle ="打开一个文件";
QString filter ="程序文件(*.h *.cpp);;文本文件(*.txt);;所有文件(*.*)";//文件过滤器
QString FilePath = QFileDialog::getOpenFileName(this,openTitle,curPath,filter);
qDebug()<<"FilePath="<<FilePath<<endl;
if(FilePath.isEmpty()){
return;
}
myCreatePublicOpenTextByIODevice(FilePath);
}
//文件读写采用IODevice方式
bool MainWindow::myCreatePublicOpenTextByIODevice(const QString &FilenamePath)
{
QFile file(FilenamePath);
if(!file.exists())//文件不存在
{
qDebug()<<file.errorString()<<endl;
return false;
}
//打开文件:只读,识别\n符
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
return false;//文件打开失败
//追加方式显示
//ui->plainTextEdit->appendPlainText(file.readAll());//一次性读完
//每次读取有一行
qDebug()<<"filename="<<file.fileName()<<endl;
while(!file.atEnd())
{
ui->plainTextEdit->appendPlainText(file.readLine());
}
file.close();//关闭文件
return true;
}
//文件另存
void MainWindow::on_filesave_clicked()
{
QString curPath =QDir::currentPath();//获取当前路径
qDebug()<<"curPath="<<curPath<<endl;//默认编译路径
QString openTitle ="另存文件";
QString filter ="程序文件(*.h *.cpp);;文本文件(*.txt);;所有文件(*.*)";//文件过滤器
QString FilePath = QFileDialog::getOpenFileName(this,openTitle,curPath,filter);
qDebug()<<"FilePath="<<FilePath<<endl;
if(FilePath.isEmpty()){
return;
}
myCreatePublicSaveFile(FilePath);
}
bool MainWindow::myCreatePublicSaveFile(const QString &FilenamePath)
{
QFile file(FilenamePath);
if(!file.exists())//文件不存在
{
qDebug()<<file.errorString()<<endl;
return false;
}
//打开文件:只读,识别\n符
if(!file.open(QIODevice::WriteOnly|QIODevice::Text))
return false;//文件打开失败
QString getText = ui->plainTextEdit->toPlainText();//获取整个内容
QByteArray textBytes = getText.toUtf8();//转为字节数组
file.write(textBytes,textBytes.size());
//file.write(textBytes,textBytes.length());//与上述一样
qDebug()<<"textBytes.size("<<textBytes.size()<<"textBytes.length()"
<<textBytes.length()<<endl;
file.close();//关闭文件
return true;
}


举报

相关推荐

0 条评论