Qt模拟C#的File类对文件进行操作

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

文件头:

#include <QFile>
#include <QString>
#include <iostream>
#include <QTextCodec>
#include <QTextStream>

enum Encoding
{
    ASCII = 0,
    Unicode = 1,
    UTF32 = 2,
    UTF7 = 3,
    UTF8 = 4,
    GBK = 5
};

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(0):
        c = QTextCodec::codecForName ("ASCII");
        break;
    case Encoding(1):
        c = QTextCodec::codecForName ("Unicode");
        break;
    case Encoding(2):
        c = QTextCodec::codecForName ("UTF-32");
        break;
    case Encoding(3):
        c = QTextCodec::codecForName ("UTF-7");
        break;
    case Encoding(4):
        c = QTextCodec::codecForName ("UTF-8");
        break;
    case Encoding(5):
        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 (0);
        text = stream.readAll ();
    }
    return text;
}

QString File::ReadAllText (QString path)
{
    return ReadAllText(path,Encoding(1));
}

QStringList File::ReadAllLines (QString path, Encoding encode)
{
    QStringList ts;
    if(Exist (path))
    {
        QFile file(path);
        QTextStream stream(&file);
        stream.setCodec (GetCode(encode));
        stream.seek (0);
        int index = 0;
        while(!stream.atEnd ())
        {
            QString t = stream.readLine (index);
            ts.append (t);
            index++;
        }
    }
    return ts;
}

QStringList File::ReadAllLines (QString path)
{
    return ReadAllLines(path,Encoding(1));
}

void File::WriteAllText (QString path, QString content, Encoding encode)
{
    Delete(path);
    Create(path);
    QFile file(path);
    QTextStream stream(&file);
    stream.seek (0);
    stream.setCodec (GetCode (encode));
    stream << content;
    file.close ();
}

void File::WriteAllText (QString path, QString content)
{
    WriteAllText(path,content,Encoding(1));
}

void File::WriteAllLines (QString path, QStringList content, Encoding encode)
{
    Delete(path);
    Create(path);
    QFile file(path);
    QTextStream stream(&file);
    stream.seek (0);
    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(1));
}

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(1));
}

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);
    }
}

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

时间: 2024-08-25 12:55:28

Qt模拟C#的File类对文件进行操作的相关文章

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

File类,是一个静态类,主要是来提供一些函数库用的.静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和打开一个文件.File类方法的参量很多时候都是路径path.File的一些方法可以返回FileStream和StreamWriter的对象.可以和他们配套使用. System.IO.File类和System.IO.FileInfo类主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间.下面通过程序实例来介绍其主要属性和方法. (1) 文件打开

01.使用File类读写文件

使用File类去读数据: 方法一:使用ReadAllBytes(); 方法 namespace _01.使用File类读写数据01 { class Program { static void Main(string[] args) { string path = @"C:\Users\zyj\Desktop\.NET base\0505.Net基础班第十二天\抽象类特点.txt"; byte[] b1=File.ReadAllBytes(path); //我们将读取的到的字节数组数据转

JAVA File类(文件的遍历,创建,删除)

File类构造函数 File f1=new File("H://asc//");//传入文件/目录的路径 File f2=new File(f1,"test.txt");//第一个参数为一个目录文件,第二个参数为要在当前f1目录下要创建的文件 file.list();  获得file文件夹下所有文件/目录的字符串数组 String []liStrings=f1.list(); for (int i = 0; i < liStrings.length; i++)

Java File类创建目录文件

一:File类创建目录创建目录是常用到的,当新增一条数据的时候需要把某些文件或者图片保存到本地时,就需要一个文件夹装着,这时候为了保存成功,不管有没有事先创建了一个文件夹,都会用到一个判断语句判断我们需要保存的目录路径是否存在,如果存在就直接保存,如果不存在就创建一个目录.一个测试例子: public class test4 { public static void main(String[] args) { File dir = new File("D:/test"); if (!d

JAVA实现File类中的遍历操作并输出内容

package shb.java.testIo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.Writer; import java.util.HashMap; /** * File类操作(此代码效率不是很高———>输出格式有欠缺.功能还是

Java File类(文件的读取,写入,复制与重命名)

文件的重命名   file.reNameTo() public boolean renameTo(File dest) 重新命名此抽象路径名表示的文件. 此方法行为的许多方面都是与平台有关的:重命名操作无法将一个文件从一个文件系统移动到另一个文件系统,dest为新命名的抽象文件 public boolean ReName(String path,String newname) {//文件重命名 //Scanner scanner=new Scanner(System.in); File file

字符串形的Path类的文件管理的File类的方法与应用

今天是我学习C#基础的第13天,可以说马上就要结束这个基础课程,感觉学习的理论性的我不能说全部掌握了,我只想说在思路上面的语法以及用法我应该基本掌握了,感觉效果不错,不得不说,要想在一种语言上面有大的突破,基础的还是很重要的,所以从基础学起,我认为我的选择还是对的.而且我选择了在云和学院学习,感觉效果挺好的. 今天总结了昨天讲的不熟悉的泛型的东西,今天在我们的博客园里面看到了一位博客友友写的泛型的类,写的好清晰呀,感觉博客园是个好地方,那时间刚开始加入博客园认为每天要写博客好累啊,现在感觉挺好的

.net对文件的操作之对文件目录的操作

.NET 提供一个静态File类用于文件的操作,下面列出它的主要操作方法. 返回值类型 方法名称 说明 bool Exists(string path) 用于检查指定文件是否存在 void Copy(string sourceFilePath,string destinationFilePath) 将指定路径的源文件中的内容复制到目标文件中,如果目标文件不存在,则在指定的路径中新建一个文件 void Move(string sourceFileName,string destFileName)

黑马程序员——【Java基础】——File类、Properties集合、IO包中的其他类

---------- android培训.java培训.期待与您交流! ---------- 一.File类 (一)概述 1.File类:文件和目录路径名的抽象表现形式 2.作用: (1)用来将文件或文件夹封装成对象 (2)方便于对“文件”与“文件夹属性信息”进行操作 (3)File对象,可以作为参数传递给流的构造函数 (二)构造方法 * 通过File的构造函数创建File对象 方式1:File f = new File("c:\\a.txt"); 方式2:File f2 = newF