1、QIODevice
直接继承自QObject, 是输入/输出设备的抽象类。提供了公共实现和抽象接口用于读写块数据。
Q3Socket,
Q3SocketDevice,
QAbstractSocket,
QBuffer,
QFile,
QLocalSocket,
QNetworkReply,
QProcess是它的子类。
2、QFile
打开文件:
bool open(OpenMode flags) override;
bool open(FILE *f, OpenMode ioFlags, FileHandleFlags handleFlags=DontCloseHandle);
bool open(int fd, OpenMode ioFlags, FileHandleFlags handleFlags=DontCloseHandle);
读文件:
qint64 read(char *data, qint64 maxlen);
QByteArray read(qint64 maxlen);
QByteArray readAll();
qint64 readLine(char *data, qint64 maxlen);
QByteArray readLine(qint64 maxlen = 0);
写文件:
qint64 write(const char *data, qint64 len);
qint64 write(const char *data);
inline qint64 write(const QByteArray &data);
3、QTextStream
读文件:
QString readLine(qint64 maxlen = 0);
bool readLineInto(QString *line, qint64 maxlen = 0);
QString readAll();
QString read(qint64 maxlen);
运算符重载读写操作:
QTextStream &operator>>(QChar &ch);
…若干
QTextStream &operator<<(QChar ch);
…若干
例:
QFile myFile(“test.txt”);
myFile.open(QIODevice::WriteOnly);
QTextStream textStream(&myFile);
textStream<<“this is 1st line.\r\n”;
textStream<<“this is 2nd line.\r\n”;
textStream<<“this is 3rd line.\r\n”;
textStream.flush();
myFile.close();
4、QDataStream
运算符重载读写操作:
QDataStream &operator>>(qint8 &i);
…若干
QDataStream &operator<<(qint8 i);
…若干
例:
QFile file(“test.dat”);
file.open(QIODevice::WriteOnly);
QDataStream ds(&file);
ds << 1 << 2 << “123123”;