Newer
Older
*
* @author Akira Ohgaki <akiraohgaki@gmail.com>
* @copyright Akira Ohgaki
#include <QIODevice>
#include <QTextStream>
#include <QFile>
File::File(const QString &path, QObject *parent)
: QObject(parent), path_(path)
File::File(const File &other, QObject *parent)
: QObject(parent)
{
setPath(other.path());
}
File &File::operator =(const File &other)
{
setPath(other.path());
return *this;
}
QString File::path() const
{
return path_;
}
void File::setPath(const QString &path)
{
path_ = path;
}
bool File::exists()
{
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
}
QByteArray File::readData()
{
QByteArray data;
QFile file(path());
if (file.exists() && file.open(QIODevice::ReadOnly)) {
data = file.readAll();
file.close();
}
return data;
}
bool File::writeData(const QByteArray &data)
{
QFile file(path());
if (file.open(QIODevice::WriteOnly)) {
file.write(data);
file.close();
return true;
}
return false;
}
QString File::readText()
{
QString data;
QFile file(path());
if (file.exists() && file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
in.setCodec("UTF-8");
data = in.readAll();
file.close();
}
return data;
}
bool File::writeText(const QString &data)
{
QFile file(path());
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out.setCodec("UTF-8");
out << data;
file.close();
return true;
}
return false;
}
bool File::copy(const QString &newPath)
{