其实只是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类对文件进行操作的更多相关文章

  1. 九:File类,文件的操作

    File的常用方法:

  2. c# 命令行下编译c#文件 // c# file类读写文件

    c# 命令行下编译c#文件 2010-03-01 15:02:14|  分类: c# 学习|字号 订阅     在 开始  ——>程序 ——>vstool中打开vs2008命令提示. 通过 ...

  3. 【转载】 C#通过File类实现文件拷贝复制的功能

    在Windows系统的使用过程中,一个较常使用的功能就是文件的复制拷贝操作,其实在C#开发中,也可以使用File类库中的Copy方法来实现文件的拷贝,支持设定原文件地址,以及拷贝复制后的文件存放路径. ...

  4. Java 之 File类(文件操作)

    一.概述 java.io.File 类是文件和目录路径名册抽象表示,主要用于文件和目录的创建.查找和删除等操作. File类是一个与系统无关的类,任何的操作系统都可以使用这个类中的方法. 路径问题: ...

  5. C#File类常用文件操作以及一个模拟的控制台文件管理系统

    重温一下C#中File类的一些基本操作: File类,是一个静态类,主要是来提供一些函数库用的. 使用时需要引入System.IO命名空间. 一.常用操作: 1.创建文件方法 //参数1:要创建的文件 ...

  6. C#中File类的文件操作方法详解

    File类,是一个静态类,主要是来提供一些函数库用的.静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和打开一个文件.File类方法的参量很多时候都是路径path.F ...

  7. Java File类与文件IO流总结

    1.File类 File类被定义为“文件和目录路径名的抽象表示形式”,这是因为File类既可以表示“文件”也可以表示“目录”,他们都通过对应的路径来描述.通过构造函数创建一个File类对象,则该对象就 ...

  8. File类、文件过滤器、递归、文件及文件夹的操作方法

    一.File Io概述: 当需要把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作. 当把持久设备上的数据读取到内存中的这个动作称为输入(读)Input操作. 因此我们把这种输入和 ...

  9. 01.使用File类读写文件

    使用File类去读数据: 方法一:使用ReadAllBytes(); 方法 namespace _01.使用File类读写数据01 { class Program { static void Main ...

随机推荐

  1. mysqldump的流程

    发布时间:2013 年 4 月 6 日 发布者: OurMySQL 来源:P.Linux Laboratory      前几天看到群里在讨论mysqldump导致锁表的问题,为什么一个表已经dump ...

  2. show engine innodb status解读

    xiaoboluo768   注:以下内容为根据<高性能mysql第三版>和<mysql技术内幕innodb存储引擎>的innodb status部分的个人理解,如果有错误,还 ...

  3. SQLite数据库中获取新插入数据的自增长ID

    SQLite数据库中有一有列名为ID的自增列,项目需求要在向数据库在插入新数据的同时返回新插入数据行的ID. 我这里用事务,把插入和查询语句通过ExecuteReader一起提交,返回DbDataRe ...

  4. rabbitmq——用户管理

    安装最新版本的rabbitmq(3.3.1),并启用management plugin后,使用默认的账号guest登陆管理控制台,却提示登陆失败. 翻看官方的release文档后,得知由于账号gues ...

  5. 怎样用VB自动更新应用程序

    具体程序实现如下:在应用程序工程MyApp中的部分代码如下:Option Explicit'编译后的应用程序名称,注意没有后缀 .EXE,本例为MYAPPPrivate Const App_Name ...

  6. C语言宏定义相关

    写好C语言,漂亮的宏定义很重要,使用宏定义可以防止出错,提高可移植性,可读性,方便性 等等.下面列举一些成熟软件中常用得宏定义......1,防止一个头文件被重复包含#ifndef COMDEF_H# ...

  7. 对于android触摸事件模型的一些理解

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  8. Adobe Edge Animate –使用EdgeCommons加载和播放音频

    Adobe Edge Animate –使用EdgeCommons加载和播放音频 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. 在Edge中,可以new一 ...

  9. CF Theatre Square

    Theatre Square time limit per test 2 seconds memory limit per test 64 megabytes input standard input ...

  10. 显示创建一个表的SQL语句

    显示创建数据库中包的语句,从而可以方便的对表的结构进行修改和复制(当然还有其他的方式) 显示表结构: 显示创建表语句: show create table tablename;