Qt读写文件(2种方式)实现详解

2020-08-19 11:18:52 浏览数 (1)

FileWR.h

代码语言:javascript复制
#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_FileWR.h"

class FileWR : public QMainWindow
{
  Q_OBJECT

public:
  FileWR(QWidget *parent = Q_NULLPTR);

private:
  Ui::FileWRClass ui;

private slots:
  void on_pushButton_clicked();
  bool FileWR::openTextByIODevice(const QString &aFileName);
  bool FileWR::saveTextByIODevice(const QString &aFileName);
  bool FileWR::saveTextByStream(const QString &aFileName);
  bool FileWR::openTextByStream(const QString &aFileName);
};

FileWR.cpp

代码语言:javascript复制
#include "FileWR.h"
#include <qdir.h>
#include <qfiledialog.h>
#include <qmessagebox.h>
#include <qtextstream.h>

//加上这三句可解决中文乱码,但是不可以和QString::fromLocal8Bit或QStringLiteral同时使用
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

FileWR::FileWR(QWidget *parent)
  : QMainWindow(parent)
{
  ui.setupUi(this);
}
void FileWR::on_pushButton_clicked()
{//打开文件

  QString curPath = QDir::currentPath();//获取系统当前目录
  QString dlgTitle = "打开一个文件"; //对话框标题
  QString filter = "程序文件(*.h *.cpp);;文本文件(*.txt);;所有文件(*.*)"; //文件过滤器
  QString aFileName = QFileDialog::getOpenFileName(this, dlgTitle, curPath, filter);//全路径 C:UserszlssourcereposFileWRFileWR1.txt
  //QString aFileName = QFileDialog::getSaveFileName(this, dlgTitle, curPath, filter);
  //QMessageBox::information(this, "提示", aFileName);
  if (aFileName.isEmpty())
    return;
  openTextByIODevice(aFileName);
  saveTextByIODevice(curPath "\2.txt");
  saveTextByIODevice(curPath   "\3.txt");
  openTextByStream(curPath   "\4.txt");
}
bool FileWR::openTextByIODevice(const QString &aFileName)
{//用IODevice方式打开文本文件
  QFile   aFile(aFileName);
  if (!aFile.exists()) //文件不存在
    return false;
  if (!aFile.open(QIODevice::ReadOnly | QIODevice::Text))
    return false;
  ui.textEdit->setPlainText(aFile.readAll());// .对象 ->指针
  aFile.close();

  //ui->textEditDevice->setPlainText(aFile.readAll());
  //aFile.close();
  //ui->tabWidget->setCurrentIndex(0);
  return  true;
}
bool FileWR::saveTextByIODevice(const QString &aFileName)
{ //用IODevice方式保存文本文件
  QFile aFile(aFileName);
  //aFile.setFileName(aFileName);
  if (!aFile.open(QIODevice::WriteOnly | QIODevice::Text))
    return false;
  QString str = ui.textEdit->toPlainText();//整个内容作为字符串
  QByteArray  strBytes = str.toUtf8();//转换为字节数组
  //QByteArray  strBytes=str.toLocal8Bit();
  aFile.write(strBytes, strBytes.length());  //写入文件
  aFile.close();
  //ui->tabWidget->setCurrentIndex(0);
  return true;
}

bool FileWR::saveTextByStream(const QString &aFileName)
{//用QTextStream保存文本文件
  QFile aFile(aFileName);
  if (!aFile.open(QIODevice::WriteOnly | QIODevice::Text))
    return false;
  QTextStream aStream(&aFile); //用文本流读取文件
  QString str = ui.textEdit->toPlainText(); //转换为字符串
  aStream << str; //写入文本流
  aFile.close();//关闭文件
  return  true;
}
bool FileWR::openTextByStream(const QString &aFileName)
{ //用 QTextStream打开文本文件
  QFile aFile(aFileName);
  if (!aFile.exists()) //文件不存在
    return false;
  if (!aFile.open(QIODevice::ReadOnly | QIODevice::Text))
    return false;
  QTextStream aStream(&aFile); //用文本流读取文件
  aStream.setAutoDetectUnicode(true); //自动检测Unicode,才能正常显示文档内的汉字
  //ui.textEdit->setPlainText(aStream.readAll());
  ui.textEdit->clear();//清空
  QString str;
  while (!aStream.atEnd())
  {
    str = aStream.readLine();//读取文件的一行
    ui.textEdit->append(str); //添加到文本框显示
    
  }
  aFile.close();//关闭文件
  //ui->tabWidget->setCurrentIndex(1);
  return  true;
}

运行结果:

参考链接:

http://c.biancheng.net/view/1877.html

qt

0 人点赞