Qt模拟C#的File类对文件进行操作
其实只是QT菜鸟为了练习而搞出来的
文件头:
- #include <QFile>
- #include <QString>
- #include <iostream>
- #include <QTextCodec>
- #include <QTextStream>
- enum Encoding
- {
- ASCII = ,
- Unicode = ,
- UTF32 = ,
- UTF7 = ,
- UTF8 = ,
- GBK =
- };
- class File
- {
- public:
- File();
- static void Create(QString path); //创建文件,如果文件存在则覆盖
- static bool Exist(QString path); //判断文件是否存在
- static void AppendAllText(QString path, QString content, Encoding encode); //向现有文件内追加数据
- static void AppendAllText(QString path, QString content); //向现有文件内追加数据
- static void AppendAllLines(QString path,QStringList lines, Encoding encode); //向现有文件内追加多行数据
- static void AppendAllLines(QString path,QStringList lines); //向现有文件内追加多行数据
- static void WriteAllText(QString path,QString content,Encoding encode); //创建新文件并写入文件,如果有文件则覆盖
- static void WriteAllText(QString path,QString content); //创建新文件并写入文件,如果有文件则覆盖
- static void WriteAllLines(QString path,QStringList content,Encoding encode); //创建新文件并写入多行数据,如果文件存在则覆盖
- static void WriteAllLines(QString path,QStringList content); //创建新文件并写入多行数据,如果文件存在则覆盖
- static QString ReadAllText(QString path,Encoding encode); //读取文件
- static QString ReadAllText(QString path); //读取文件
- static QStringList ReadAllLines(QString path,Encoding encode); //读取文件内所有的行
- static QStringList ReadAllLines(QString path); //读取文件内所有的行
- static void Copy(QString sourceFileName,QString destFileName,bool overwrite); //拷贝文件
- static void Copy(QString sourceFileName,QString destFileName); //拷贝文件
- static void Move(QString sourceFileName,QString destFileName); //移动文件
- static void Delete(QString path); //删除文件
- static void Rename(QString path,QString name); //重命名
- private:
- static QTextCodec* GetCode(Encoding code);
- };
源文件:
- File::File()
- {
- }
- bool File::Exist (QString path)
- {
- QFile file(path);
- return file.exists ();
- }
- QTextCodec* File::GetCode (Encoding code)
- {
- QTextCodec* c;
- switch(code)
- {
- case Encoding():
- c = QTextCodec::codecForName ("ASCII");
- break;
- case Encoding():
- c = QTextCodec::codecForName ("Unicode");
- break;
- case Encoding():
- c = QTextCodec::codecForName ("UTF-32");
- break;
- case Encoding():
- c = QTextCodec::codecForName ("UTF-7");
- break;
- case Encoding():
- c = QTextCodec::codecForName ("UTF-8");
- break;
- case Encoding():
- c = QTextCodec::codecForName ("GBK");
- break;
- }
- return c;
- }
- void File::Create (QString path)
- {
- Delete(path);
- QFile file(path);
- file.open (QIODevice::WriteOnly);
- file.close ();
- }
- QString File::ReadAllText (QString path, Encoding encode)
- {
- QString text;
- if(Exist (path))
- {
- QFile file(path);
- QTextStream stream(&file);
- stream.setCodec (GetCode(encode));
- stream.seek ();
- text = stream.readAll ();
- }
- return text;
- }
- QString File::ReadAllText (QString path)
- {
- return ReadAllText(path,Encoding());
- }
- QStringList File::ReadAllLines (QString path, Encoding encode)
- {
- QStringList ts;
- if(Exist (path))
- {
- QFile file(path);
- QTextStream stream(&file);
- stream.setCodec (GetCode(encode));
- stream.seek ();
- int index = ;
- while(!stream.atEnd ())
- {
- QString t = stream.readLine (index);
- ts.append (t);
- index++;
- }
- }
- return ts;
- }
- QStringList File::ReadAllLines (QString path)
- {
- return ReadAllLines(path,Encoding());
- }
- void File::WriteAllText (QString path, QString content, Encoding encode)
- {
- Delete(path);
- Create(path);
- QFile file(path);
- QTextStream stream(&file);
- stream.seek ();
- stream.setCodec (GetCode (encode));
- stream << content;
- file.close ();
- }
- void File::WriteAllText (QString path, QString content)
- {
- WriteAllText(path,content,Encoding());
- }
- void File::WriteAllLines (QString path, QStringList content, Encoding encode)
- {
- Delete(path);
- Create(path);
- QFile file(path);
- QTextStream stream(&file);
- stream.seek ();
- stream.setCodec (GetCode (encode));
- for ( QStringList::Iterator it = content.begin(); it != content.end(); ++it )
- stream << *it << "\r\n";
- file.close ();
- }
- void File::WriteAllLines (QString path, QStringList content)
- {
- WriteAllLines(path,content,Encoding());
- }
- void File::AppendAllText (QString path, QString content, Encoding encode)
- {
- if(!Exist (path))Create(path);
- QString text = ReadAllText(path, encode);
- text += content;
- WriteAllText(path,text,encode);
- }
- void File::AppendAllText (QString path, QString content)
- {
- AppendAllText(path,content,Encoding());
- }
- void File::AppendAllLines (QString path, QStringList lines, Encoding encode)
- {
- if(!Exist (path))Create(path);
- QString text = ReadAllText(path, encode);
- text += "\r\n";
- foreach(QString s,lines)
- text += (s + "\r\n");
- WriteAllText(path,text,encode);
- }
- void File::Copy (QString sourceFileName, QString destFileName, bool overwrite)
- {
- if(Exist (destFileName) && overwrite)
- {
- QFile file(destFileName);
- file.remove ();
- }
- if(!Exist (destFileName))
- {
- QFile::copy (sourceFileName,destFileName);
- }
- }
- void File::Copy (QString sourceFileName, QString destFileName)
- {
- Copy(sourceFileName,destFileName,false);
- }
- void File::Delete (QString path)
- {
- QFile file(path);
- if(file.exists ())
- {
- file.remove ();
- }
- }
- void File::Rename (QString path, QString name)
- {
- if(Exist(path))
- {
- QFile file(path);
- QString oldName = file.fileName ();
- QFile::rename (oldName,name);
- }
- }
花了不少时间搞定了,不过并没有做测试,有没有问题我也不知道……
Qt模拟C#的File类对文件进行操作的更多相关文章
- 九:File类,文件的操作
File的常用方法:
- c# 命令行下编译c#文件 // c# file类读写文件
c# 命令行下编译c#文件 2010-03-01 15:02:14| 分类: c# 学习|字号 订阅 在 开始 ——>程序 ——>vstool中打开vs2008命令提示. 通过 ...
- 【转载】 C#通过File类实现文件拷贝复制的功能
在Windows系统的使用过程中,一个较常使用的功能就是文件的复制拷贝操作,其实在C#开发中,也可以使用File类库中的Copy方法来实现文件的拷贝,支持设定原文件地址,以及拷贝复制后的文件存放路径. ...
- Java 之 File类(文件操作)
一.概述 java.io.File 类是文件和目录路径名册抽象表示,主要用于文件和目录的创建.查找和删除等操作. File类是一个与系统无关的类,任何的操作系统都可以使用这个类中的方法. 路径问题: ...
- C#File类常用文件操作以及一个模拟的控制台文件管理系统
重温一下C#中File类的一些基本操作: File类,是一个静态类,主要是来提供一些函数库用的. 使用时需要引入System.IO命名空间. 一.常用操作: 1.创建文件方法 //参数1:要创建的文件 ...
- C#中File类的文件操作方法详解
File类,是一个静态类,主要是来提供一些函数库用的.静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和打开一个文件.File类方法的参量很多时候都是路径path.F ...
- Java File类与文件IO流总结
1.File类 File类被定义为“文件和目录路径名的抽象表示形式”,这是因为File类既可以表示“文件”也可以表示“目录”,他们都通过对应的路径来描述.通过构造函数创建一个File类对象,则该对象就 ...
- File类、文件过滤器、递归、文件及文件夹的操作方法
一.File Io概述: 当需要把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作. 当把持久设备上的数据读取到内存中的这个动作称为输入(读)Input操作. 因此我们把这种输入和 ...
- 01.使用File类读写文件
使用File类去读数据: 方法一:使用ReadAllBytes(); 方法 namespace _01.使用File类读写数据01 { class Program { static void Main ...
随机推荐
- IOS横竖屏控制与事件处理
公司App里面有个需求,即所有界面都是竖屏,且不允许横屏切换,唯独有一个图表界面允许横屏.那么,根据此需求处理如下: 首先,确保App本身应该允许转屏切换: 再次,我的App里面都是走UINaviga ...
- SQLServer 之 2008还原的时候无法获得对数据库的独占访问权解决
SQL2008还原的时候无法获得对数据库的独占访问权解决 还原或删除sql server 2008数据库时,经常会出现: “因为数据库正在使用,所以无法获得对数据库的独占访问权”,终解决方案如下 // ...
- DataBase 之 表操作
1:向表中添加字段 Alter table [表名] add [列名] 类型 2: 删除字段 Alter table [表名] drop column [列名] 3: 修改表中字段类型 (可以修改列的 ...
- 英文破折号(em dash)、连接号(en dash)与连字符(hyphen)的区别及各自用法是什么?
英文破折号(em dash).连接号(en dash)与连字符(hyphen)的区别及各自用法是什么?在科技写作中有何特点? 2 条评论 分享 按票数排序按时间排序 6 个回答 赞同85反对, ...
- 使用JS制作一个鼠标可拖的DIV(二)——限制区域移动
这次是要对上一篇的内容进行扩展. 由于需要对可拖动的 DIV 进行一个区域范围的限制,所以要给于一个容器,让可拖动的 DIV 元素不能逃出该容器的大小范围. 一.思路 1.在外层增加一个 DIV 容器 ...
- Android(java)学习笔记109:通过反射获取成员变量和成员方法并且使用
一.反射获取成员变量并且使用: 1.获取字节码文件对象: Class c = Class.forName("cn.itcast_01.Person"); 2.使用无 ...
- envi5.1下载地址
ENVI 5.1 installer 32 bit :链接: http://pan.baidu.com/s/1c0EGZIw 密码: gcogENVI 5.1 Installer 64 bit :链接 ...
- CSS skills: 3) show sub-navigate items when mouse hove on nav-item
<header> <div class="am-g am-g-fixed"> <ul class="am-avg-lg-2 topbar f ...
- mongoDB 插入数据 用java实现
import java.net.UnknownHostException; import com.mongodb.BasicDBObject; import com.mongodb.DB; impor ...
- CentOS(十)--与Linux文件和目录管理相关的一些重要命令②
在结束了第二期的广交会实习之后,又迎来了几天休闲的日子,继续学习Linux.在上一篇随笔 Linux学习之CentOS(十七)--与Linux文件和目录管理相关的一些重要命令① 中,详细记录了与Lin ...