QFile QDataStream QTextStream
#include <QCoreApplication> #include <QMap> #include <QFile> #include <QDir> #include <QDebug> #include <QTextStream> #include <QDataStream> #include <QLinkedList> void qDirTest() { QDir dir("F:/NVIDIA"); qDebug() << dir.exists() ; qDebug() << dir.absolutePath(); dir.cdUp(); qDebug() << dir.absolutePath() << "\n-------"; dir.cd("NVIDIA"); qDebug() << dir.dirName(); QString path = "E:/test_dir"; QDir dir_test(path); if(!dir_test.exists()) { dir_test.mkdir(path); } dir_test.rmdir("E:/test_dir"); // Show The Drive of files QString dir_path = "E:/"; QDir dir_list(dir_path); foreach( QFileInfo item,dir_list.entryInfoList()) { if(item.isFile()) { qDebug() << item.absoluteFilePath() <<" --->is File"; } if(item.isDir()) { qDebug() << item.absoluteFilePath() <<" --->is Dir"; } } } // QFile TEST namespace QFileTextDataStream { void write_text_stream(QString name) { QFile wFile(name); if(!wFile.open(QFile::WriteOnly | QFile::Text)) // IT's FILE * { qDebug () << "open files error"; } QTextStream stream(&wFile); stream << "Hello World\n" << "This is jack"; wFile.flush(); wFile.close(); } void read_text_stream(QString name) { QFile rFile(name); if(!rFile.open(QFile::ReadOnly | QFile::Text)) // IT's FILE * { qDebug () << "open files error"; } QTextStream in(&rFile); QString data = in.readAll(); qDebug() << data; } void QFileTextTest(QString FileName) { qDebug () << "write file test"; write_text_stream(FileName); qDebug() << "now Reading"; read_text_stream(FileName); } void write_binary_stream(QString name) { QFile file(name); if(!file.open(QIODevice::WriteOnly)) { qDebug () << "error"; } QDataStream out(&file); out << QString("Houdini ") << 5; file.flush(); file.close(); } void read_binary_stream(QString name) { QFile file(name); if(!file.open(QIODevice::ReadOnly)) { qDebug () << "error"; } QDataStream read(&file); QString fname; int val; read >> fname >> val; qDebug() << fname<<":" << val; file.flush(); file.close(); } } namespace DataCore { void data_struction_test() { QMap<int ,QString> map; map.insert(10,"Houdini"); map.insert(11,"nuke"); map.insert(12,"maya"); foreach (int i,map.keys()) { qDebug() << map[i] ; } } } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //DirTest(); //QFileTextDataStream::QFileTest("F:/test.txt"); QFileTextDataStream::write_binary_stream("F:/test.binary"); QFileTextDataStream::read_binary_stream("F:/test.binary"); //DataCore::data_struction_test(); return a.exec(); }
CopyFile Method 1:
// QFile And QDataStream int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QFile file("F:/dj.mp3"); if(!file.open(QFile::ReadOnly)){ perror("error open\n"); return 1; } QFile copy_file("F:/copytest.mp3"); if(!copy_file.open(QFile::WriteOnly)){ perror("error write open\n"); return 1; } QDataStream from_data(&file); QDataStream to_data(©_file); int buffer_size = 1024; char data[buffer_size]; while(!file.atEnd()) { from_data.readRawData(data,buffer_size); to_data.writeRawData(data,buffer_size); } file.close(); copy_file.close(); fprintf(stdout,"%s \n","end reading"); return a.exec(); }
CopyFile Method 2:
QCoreApplication a(argc, argv); QFile file("F:/houdini-13.0.509.exe"); if(!file.open(QFile::ReadOnly)) { perror("error open\n"); return 1; } QFile copy_file("F:/copytest.exe"); if(!copy_file.open(QFile::WriteOnly)) { perror("error write open\n"); return 1; } QByteArray line; while(!file.atEnd()) { line.clear(); line =file.read(1000); copy_file.write(line); } fprintf(stdout,"%s \n","end reading"); file.close(); copy_file.close(); return a.exec();
DataStream And ByteArray
#include <QCoreApplication> #include <QDataStream> #include <QByteArray> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QByteArray block; QDataStream stream(&block,QIODevice::WriteOnly); stream<<quint16(0) << QString("Houdini")<<QString("Maya"); stream.device()->seek(0); stream<<quint16(15); qDebug() << block; // read back QDataStream stream_read(&block,QIODevice::ReadOnly); quint16 val; QString name; QString func; stream_read>>val >>name >>func; qDebug() << val << name <<func; return a.exec(); }
分割文件流:)
void get_buffer_split2(QByteArray readInArray,QList<QByteArray> &data,int buffer_size) { float t = buffer_size; int num = ceil(float(readInArray.size())/float(t)); for(int i=1;i<=num;i++) { QByteArray _temp = readInArray.left(buffer_size); readInArray.remove(0,buffer_size); data.push_back(_temp); } } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QByteArray readArray; QFile readFile("/home/gearslogy/icon.png"); if(!readFile.open(QFile::ReadOnly)) { qDebug() << "error read file"; readFile.close(); return 2; } readArray = readFile.readAll(); QList<QByteArray> split_data; get_buffer_split2(readArray,split_data,10); QFile writeFile("/home/gearslogy/iconCopy.png"); if(!writeFile.open(QFile::WriteOnly)) { qDebug() << "write file error"; writeFile.close(); return 0; } for(int i=0;i<split_data.size();i++) { QByteArray _temp = split_data[i]; writeFile.write(_temp); } writeFile.close(); return a.exec(); }