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 ...
随机推荐
- Arrays.sort(new String[]{"aaa"}); 排序方法
private static void mergeSort(Object[] src, Object[] dest, int low, int high, int off) { int length ...
- slf4j-simple的配置
slf4j-simple是一款日志框架 它既可通过VM arguments来配置也可通过在classpath放置simplelogger.properties文件来配置 通过VM arguments来 ...
- Eclipse的java代码出错:The import org.apache cannot be resolved
Eclipse中,折腾java代码. 把之前在android中的代码拿过来使用. 结果出现The import org.apache cannot be resolved的错误: [解决过程] 1.这 ...
- 小白日记38:kali渗透测试之Web渗透-手动漏洞挖掘(四)-文件上传漏洞
手动漏洞挖掘 文件上传漏洞[经典漏洞,本身为一个功能,根源:对上传文件的过滤机制不严谨] <?php echo shell_exec($_GET['cmd']);?> 直接上传webshe ...
- 小白日记10:kali渗透测试之端口扫描-UDP、TCP、僵尸扫描、隐蔽扫描
端口扫描 二三四层发现的目的只是为了准确发现所有活着主机IP,确定攻击面,端口扫描即发现攻击点,发现开放端口.端口对应网络服务及应用端程序,服务端程序的漏洞通过端口攻入.[所有的扫描结果,都不要完全相 ...
- ThreadPoolExecutor运转机制详解
ThreadPoolExecutor运转机制详解 - 走向架构师之路 - 博客频道 - CSDN.NET 最近发现几起对ThreadPoolExecutor的误用,其中包括自己,发现都是因为没有仔细看 ...
- git 在windows上 生成ssh公钥
今天上传代码到服务器时,报如下错误: 上网搜了一下,应该是ssh过期了.我们就来生成新的ssh公钥吧. 1. 打开git bash 2. 输入命令: ssh-keygen -t rsa ...
- 进程间通信之管道(pipe、fifo)
我们先来说说进程间通信(IPC)的一般目的,大概有数据传输.共享数据.通知事件.资源共享和进程控制等.但是我们知道,对于每一个进程来说这个进程看到属于它的一块内存资源,这块资源是它所独占的,所以进程之 ...
- CentOS(一)--CentOS6.4环境搭建
一.前言 作为一个想从事j2ee后台开发的程序猿,linux系统怎能不学呢?所以,这几天自己准备学习一下linux操作系统.废话不多说,直奔主题. 要学linux开发,首先得要安装linux系统吧,这 ...
- [改善Java代码]用枚举实现工厂方法模式更简洁
工厂方法模式(Factory Method Patter)是"创建对象的接口",让子类决定实例化哪一个类,并使一个类的实例化延迟到其子类.工厂方法模式在我们的开发工作中,经常会用到 ...