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 ...
随机推荐
- 步进循环语句for
一.for语句的基本格式 for 变量 in 列表 do 语句块 done 二.使用for语句处理列表(数组) [root@localhost shell]# cat use_for_deal_wit ...
- mysql修改表结构
表 linksus_gov_running_trans 和 linksus_gov_running 的 is_mulsplit_id 字段需要改成 bigint(20)原:`is_mulsplit_i ...
- PhpCMS标签:专题模块special标签
专题模块 专题模块PC标签调用说明 模块名:special 模块提供的可用操作 操作名 说明 lists 专题列表 content_list 专题信息列表 hits 专题信息点击排序 下面对所有的操作 ...
- java_小技巧
看很多人说,在Eclipse里面,输入Syso然后按 ALT+/不起作用. 正确的用法如下,先输入一行 System.out.println(); 然后连按5次以上shift键,其实就是粘滞的功能.接 ...
- Linux编程之《守护进程》
Intro ----- 守护进程,也就是通常说的Daemon进程,是Linux中的后台服务进程.它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程常 ...
- Samba服务详解
Samba文件服务器 本章结构 服务简介 SMB协议 Server Message Block,服务消息块 CIFS协议 Common Internet File System,通用互联网文件系统 S ...
- 关于Eclipse的Save时的自定义操作
Eclipse中Save+S的时候会包含很多其他操作.例如代码格式化,自动导入或者删除jar包等等. 如果修改代码的时候不想让Eclipse做这些操作,有两种方法. 第一: 用一个文本编辑器打开这个文 ...
- 关于JDK中的集合总结(三)
泛型: jdk1.5出现的安全机制. 好处: 1,将运行时期的问题ClassCastException转到了编译时期. 2,避免了强制转换的麻烦. <>:什么时候用?当操作的引用数据类型不 ...
- 钓鱼 贪心 end
#include<iostream> int m,n; int *p; int dis=0; int peo=0; int data[3][2]; int b[3][2]; int da[ ...
- 嵌入式开发笔记 - U-Boot相关
1.U-boot使用准备 1.1 U-boot下载 通过德国的denx软件中心提供的FTP下载合集,下载网址: ftp://ftp.denx.de/pub/u-boot/