Symbian 檔案流儲存簡單操作(外化和內化)

來源:互聯網
上載者:User

 

class MyObject:public CBase
{
public:
 static MyObject* NewL();
 static MyObject* NewLC();
 // ~MyObject();
public:
 void SetName(const TDes& name);
 TDes GetName()const;
 void SetId(TUint id);
 TUint GetID()const;
 void SetValue(TInt value);
 TInt GetValue()const;
private:
 void Construct();
 MyObject();
public:
  //這兩個方法必有實現,主要用於讀和寫整個對象時,
 //通過調用這個兩個方法實現對象內部資料的讀或者寫操作
 void     ExternalizeL(RWriteStream& aStream) const;
 void     InternalizeL(RReadStream& aStream);
private:
 TBuf<100> iClassName;
 TUint16 id;
 TInt32 value;
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//implement....................................................................................................//

MyObject::MyObject():id(0),value(0)
{
 iClassName.Zero();
}

MyObject* MyObject::NewL()
 {
  MyObject* self=MyObject::NewLC();
  CleanupStack::Pop(self);
  return self;
 }

MyObject* MyObject::NewLC()
 {
  MyObject* self=new (ELeave) MyObject;
  CleanupStack::PushL(self);
  self->Construct();
  return self;
 }

void MyObject::Construct()

}

void MyObject::SetName(const TDes& name)
{
 iClassName=name;
}

TDes MyObject::GetName()const
{
 return iClassName;
}
void MyObject::SetId(TUint id)
{
 this->id=id;
}
TUint MyObject::GetID()const
{
 return id;
}
void MyObject::SetValue(TInt value)
{
 this->value=value;
}
TInt MyObject::GetValue()const
{
 return value;
}

void  MyObject::ExternalizeL(RWriteStream& aStream) const
{
 aStream.WriteUint16L(id);
 aStream.WriteInt32L(value);
 aStream<<iClassName;
}
void  MyObject::InternalizeL(RReadStream& aStream)
{
 id=aStream.ReadUint16L();
 value=aStream.ReadInt32L();
 aStream>>iClassName;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//使用樣本:

_LIT(KFileName,"c://test//file.dat");
//存於對象資料(序列化對象)
void  StoreObject()

 MyObject* obj=MyObject::NewLC();
 obj->SetId(101);
 _LIT(KContent,"myClassString");
 TBuf<100> buf=KContent;

 obj->SetName(buf);
 obj->SetValue(100000);

 RFs fs;
 fs.Connect();
 fs.MkDirAll(KFileName);
 TParse parser;
 fs.Parse(KFileName,parser);
 CFileStore* store=CDirectFileStore::ReplaceLC(fs,parser.FullName(),EFileWrite);
 store->SetTypeL(KDirectFileStoreLayoutUid);

 RStoreWriteStream outStream;
 TStreamId id=outStream.CreateLC(*store);
 outStream<<*obj; //調用obj相應的ExternalizeL(RWriteStream&)方法
 outStream.CommitL(); //提交流 

 CleanupStack::PopAndDestroy(); //clean up self

 store->SetRootL(id);
 store->CommitL(); //把檔案提交到檔案系統

// outStream.Close(); 
 CleanupStack::PopAndDestroy(2); //outStreamand store,obj

 fs.Close();

}

//讀取對象資料(還原序列化對象)
void  ReStoreObject()
{
 MyObject* self=MyObject::NewLC();

 RFs fs;
 TParse parser;
 fs.Connect();
 fs.Parse(KFileName,parser);
 CFileStore* store=CDirectFileStore::OpenLC(fs,parser.FullName(),EFileRead);
 RStoreReadStream reader;
 reader.OpenLC(*store,store->Root());
 reader>>*self; //調用obj相應的InternalizeL(RReadStream&)方法
 iObjName.Copy(self->GetName()); 
 CleanupStack::PopAndDestroy(3); //reader,store,self 
 fs.Close();
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.