其实只是QT菜鸟为了练习而搞出来的

文件头:

  1. #include <QFile>
  2. #include <QString>
  3. #include <iostream>
  4. #include <QTextCodec>
  5. #include <QTextStream>
  6.  
  7. enum Encoding
  8. {
  9. ASCII = ,
  10. Unicode = ,
  11. UTF32 = ,
  12. UTF7 = ,
  13. UTF8 = ,
  14. GBK =
  15. };
  16.  
  17. class File
  18. {
  19. public:
  20. File();
  21.  
  22. static void Create(QString path); //创建文件,如果文件存在则覆盖
  23. static bool Exist(QString path); //判断文件是否存在
  24. static void AppendAllText(QString path, QString content, Encoding encode); //向现有文件内追加数据
  25. static void AppendAllText(QString path, QString content); //向现有文件内追加数据
  26. static void AppendAllLines(QString path,QStringList lines, Encoding encode); //向现有文件内追加多行数据
  27. static void AppendAllLines(QString path,QStringList lines); //向现有文件内追加多行数据
  28. static void WriteAllText(QString path,QString content,Encoding encode); //创建新文件并写入文件,如果有文件则覆盖
  29. static void WriteAllText(QString path,QString content); //创建新文件并写入文件,如果有文件则覆盖
  30. static void WriteAllLines(QString path,QStringList content,Encoding encode); //创建新文件并写入多行数据,如果文件存在则覆盖
  31. static void WriteAllLines(QString path,QStringList content); //创建新文件并写入多行数据,如果文件存在则覆盖
  32. static QString ReadAllText(QString path,Encoding encode); //读取文件
  33. static QString ReadAllText(QString path); //读取文件
  34. static QStringList ReadAllLines(QString path,Encoding encode); //读取文件内所有的行
  35. static QStringList ReadAllLines(QString path); //读取文件内所有的行
  36. static void Copy(QString sourceFileName,QString destFileName,bool overwrite); //拷贝文件
  37. static void Copy(QString sourceFileName,QString destFileName); //拷贝文件
  38. static void Move(QString sourceFileName,QString destFileName); //移动文件
  39. static void Delete(QString path); //删除文件
  40. static void Rename(QString path,QString name); //重命名
  41.  
  42. private:
  43. static QTextCodec* GetCode(Encoding code);
  44.  
  45. };

源文件:

  1. File::File()
  2. {
  3. }
  4.  
  5. bool File::Exist (QString path)
  6. {
  7. QFile file(path);
  8. return file.exists ();
  9. }
  10.  
  11. QTextCodec* File::GetCode (Encoding code)
  12. {
  13. QTextCodec* c;
  14. switch(code)
  15. {
  16. case Encoding():
  17. c = QTextCodec::codecForName ("ASCII");
  18. break;
  19. case Encoding():
  20. c = QTextCodec::codecForName ("Unicode");
  21. break;
  22. case Encoding():
  23. c = QTextCodec::codecForName ("UTF-32");
  24. break;
  25. case Encoding():
  26. c = QTextCodec::codecForName ("UTF-7");
  27. break;
  28. case Encoding():
  29. c = QTextCodec::codecForName ("UTF-8");
  30. break;
  31. case Encoding():
  32. c = QTextCodec::codecForName ("GBK");
  33. break;
  34. }
  35. return c;
  36. }
  37.  
  38. void File::Create (QString path)
  39. {
  40. Delete(path);
  41. QFile file(path);
  42. file.open (QIODevice::WriteOnly);
  43. file.close ();
  44. }
  45.  
  46. QString File::ReadAllText (QString path, Encoding encode)
  47. {
  48. QString text;
  49. if(Exist (path))
  50. {
  51. QFile file(path);
  52. QTextStream stream(&file);
  53. stream.setCodec (GetCode(encode));
  54. stream.seek ();
  55. text = stream.readAll ();
  56. }
  57. return text;
  58. }
  59.  
  60. QString File::ReadAllText (QString path)
  61. {
  62. return ReadAllText(path,Encoding());
  63. }
  64.  
  65. QStringList File::ReadAllLines (QString path, Encoding encode)
  66. {
  67. QStringList ts;
  68. if(Exist (path))
  69. {
  70. QFile file(path);
  71. QTextStream stream(&file);
  72. stream.setCodec (GetCode(encode));
  73. stream.seek ();
  74. int index = ;
  75. while(!stream.atEnd ())
  76. {
  77. QString t = stream.readLine (index);
  78. ts.append (t);
  79. index++;
  80. }
  81. }
  82. return ts;
  83. }
  84.  
  85. QStringList File::ReadAllLines (QString path)
  86. {
  87. return ReadAllLines(path,Encoding());
  88. }
  89.  
  90. void File::WriteAllText (QString path, QString content, Encoding encode)
  91. {
  92. Delete(path);
  93. Create(path);
  94. QFile file(path);
  95. QTextStream stream(&file);
  96. stream.seek ();
  97. stream.setCodec (GetCode (encode));
  98. stream << content;
  99. file.close ();
  100. }
  101.  
  102. void File::WriteAllText (QString path, QString content)
  103. {
  104. WriteAllText(path,content,Encoding());
  105. }
  106.  
  107. void File::WriteAllLines (QString path, QStringList content, Encoding encode)
  108. {
  109. Delete(path);
  110. Create(path);
  111. QFile file(path);
  112. QTextStream stream(&file);
  113. stream.seek ();
  114. stream.setCodec (GetCode (encode));
  115. for ( QStringList::Iterator it = content.begin(); it != content.end(); ++it )
  116. stream << *it << "\r\n";
  117. file.close ();
  118. }
  119.  
  120. void File::WriteAllLines (QString path, QStringList content)
  121. {
  122. WriteAllLines(path,content,Encoding());
  123. }
  124.  
  125. void File::AppendAllText (QString path, QString content, Encoding encode)
  126. {
  127. if(!Exist (path))Create(path);
  128. QString text = ReadAllText(path, encode);
  129. text += content;
  130. WriteAllText(path,text,encode);
  131. }
  132.  
  133. void File::AppendAllText (QString path, QString content)
  134. {
  135. AppendAllText(path,content,Encoding());
  136. }
  137.  
  138. void File::AppendAllLines (QString path, QStringList lines, Encoding encode)
  139. {
  140. if(!Exist (path))Create(path);
  141. QString text = ReadAllText(path, encode);
  142. text += "\r\n";
  143. foreach(QString s,lines)
  144. text += (s + "\r\n");
  145. WriteAllText(path,text,encode);
  146. }
  147.  
  148. void File::Copy (QString sourceFileName, QString destFileName, bool overwrite)
  149. {
  150. if(Exist (destFileName) && overwrite)
  151. {
  152. QFile file(destFileName);
  153. file.remove ();
  154. }
  155. if(!Exist (destFileName))
  156. {
  157. QFile::copy (sourceFileName,destFileName);
  158. }
  159. }
  160.  
  161. void File::Copy (QString sourceFileName, QString destFileName)
  162. {
  163. Copy(sourceFileName,destFileName,false);
  164. }
  165.  
  166. void File::Delete (QString path)
  167. {
  168. QFile file(path);
  169. if(file.exists ())
  170. {
  171. file.remove ();
  172. }
  173. }
  174.  
  175. void File::Rename (QString path, QString name)
  176. {
  177. if(Exist(path))
  178. {
  179. QFile file(path);
  180. QString oldName = file.fileName ();
  181. QFile::rename (oldName,name);
  182. }
  183. }

花了不少时间搞定了,不过并没有做测试,有没有问题我也不知道……

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. IOS横竖屏控制与事件处理

    公司App里面有个需求,即所有界面都是竖屏,且不允许横屏切换,唯独有一个图表界面允许横屏.那么,根据此需求处理如下: 首先,确保App本身应该允许转屏切换: 再次,我的App里面都是走UINaviga ...

  2. SQLServer 之 2008还原的时候无法获得对数据库的独占访问权解决

    SQL2008还原的时候无法获得对数据库的独占访问权解决 还原或删除sql server 2008数据库时,经常会出现: “因为数据库正在使用,所以无法获得对数据库的独占访问权”,终解决方案如下 // ...

  3. DataBase 之 表操作

    1:向表中添加字段 Alter table [表名] add [列名] 类型 2: 删除字段 Alter table [表名] drop column [列名] 3: 修改表中字段类型 (可以修改列的 ...

  4. 英文破折号(em dash)、连接号(en dash)与连字符(hyphen)的区别及各自用法是什么?

    英文破折号(em dash).连接号(en dash)与连字符(hyphen)的区别及各自用法是什么?在科技写作中有何特点?   2 条评论 分享   按票数排序按时间排序 6 个回答 赞同85反对, ...

  5. 使用JS制作一个鼠标可拖的DIV(二)——限制区域移动

    这次是要对上一篇的内容进行扩展. 由于需要对可拖动的 DIV 进行一个区域范围的限制,所以要给于一个容器,让可拖动的 DIV 元素不能逃出该容器的大小范围. 一.思路 1.在外层增加一个 DIV 容器 ...

  6. Android(java)学习笔记109:通过反射获取成员变量和成员方法并且使用

    一.反射获取成员变量并且使用: 1.获取字节码文件对象:         Class c = Class.forName("cn.itcast_01.Person"); 2.使用无 ...

  7. envi5.1下载地址

    ENVI 5.1 installer 32 bit :链接: http://pan.baidu.com/s/1c0EGZIw 密码: gcogENVI 5.1 Installer 64 bit :链接 ...

  8. 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 ...

  9. mongoDB 插入数据 用java实现

    import java.net.UnknownHostException; import com.mongodb.BasicDBObject; import com.mongodb.DB; impor ...

  10. CentOS(十)--与Linux文件和目录管理相关的一些重要命令②

    在结束了第二期的广交会实习之后,又迎来了几天休闲的日子,继续学习Linux.在上一篇随笔 Linux学习之CentOS(十七)--与Linux文件和目录管理相关的一些重要命令① 中,详细记录了与Lin ...