一、自定义的类
代码
class TUserGroup
{
public:
TInt32 m_nGroupID;
TBuf16<20> m_bufGroupName;
void ExternalizeL(RWriteStream& aStream) const;
void InternalizeL(RReadStream& aStream);
};
void TUserGroup::ExternalizeL(RWriteStream& aStream) const
{
aStream << m_nGroupID;
aStream << m_bufGroupName;
}
void TUserGroup::InternalizeL(RReadStream& aStream)
{
aStream >> m_nGroupID;
aStream >> m_bufGroupName;
}
{
public:
TInt32 m_nGroupID;
TBuf16<20> m_bufGroupName;
void ExternalizeL(RWriteStream& aStream) const;
void InternalizeL(RReadStream& aStream);
};
void TUserGroup::ExternalizeL(RWriteStream& aStream) const
{
aStream << m_nGroupID;
aStream << m_bufGroupName;
}
void TUserGroup::InternalizeL(RReadStream& aStream)
{
aStream >> m_nGroupID;
aStream >> m_bufGroupName;
}
二、写操作
代码
//创建流式文件
TFileName fnDataFile;
fnDataFile.Copy(_L("c:\\StreamExam.dat"));
RFs& fs = CCoeEnv::Static()->FsSession();
RFileWriteStream fileWriteStream;
//User::LeaveIfError(fileWriteStream.Create(fs,fnDataFile,EFileWrite));
User::LeaveIfError(fileWriteStream.Replace(fs, fnDataFile,
EFileWrite));
fileWriteStream.PushL();
//外部化数据到文件中
TInt32 nID;
TBuf16<20> bufName;
nID = 10;
bufName.Append(_L("John"));
fileWriteStream << nID;
fileWriteStream << bufName;
//外部化存储自定义类
TUserGroup userGroup;
userGroup.m_nGroupID = 50;
userGroup.m_bufGroupName.Copy(_L("beijing"));
fileWriteStream << userGroup;
//提交更改、释放资源
fileWriteStream.CommitL();
fileWriteStream.Pop();
fileWriteStream.Release();
TFileName fnDataFile;
fnDataFile.Copy(_L("c:\\StreamExam.dat"));
RFs& fs = CCoeEnv::Static()->FsSession();
RFileWriteStream fileWriteStream;
//User::LeaveIfError(fileWriteStream.Create(fs,fnDataFile,EFileWrite));
User::LeaveIfError(fileWriteStream.Replace(fs, fnDataFile,
EFileWrite));
fileWriteStream.PushL();
//外部化数据到文件中
TInt32 nID;
TBuf16<20> bufName;
nID = 10;
bufName.Append(_L("John"));
fileWriteStream << nID;
fileWriteStream << bufName;
//外部化存储自定义类
TUserGroup userGroup;
userGroup.m_nGroupID = 50;
userGroup.m_bufGroupName.Copy(_L("beijing"));
fileWriteStream << userGroup;
//提交更改、释放资源
fileWriteStream.CommitL();
fileWriteStream.Pop();
fileWriteStream.Release();
三、读的操作
代码
//打开流式文件
TFileName fnDataFile;
fnDataFile.Copy(_L("c:\\StreamExam.dat"));
RFs& fs=CCoeEnv::Static()->FsSession();
RFileReadStream fileReadStream;
User::LeaveIfError(fileReadStream.Open(fs,fnDataFile,EFileRead));
fileReadStream.PushL();
//从文件中内部化数据
TInt32 nID;
TBuf16<20> bufName;
fileReadStream >> nID;
fileReadStream >> bufName;
RDebug::Print(_L("start\n"));
RDebug::Print(_L("ID=%d Name=%S\n"),nID,&bufName);
//内部化自定义类
TUserGroup userGroup;
fileReadStream >> userGroup;
RDebug::Print(_L("GroupID=%d GroupName=%S\n"),userGroup.m_nGroupID,&(userGroup.m_bufGroupName));
//释放资源
fileReadStream.Pop();
fileReadStream.Release();
RDebug::Print(_L("end\n"));
TFileName fnDataFile;
fnDataFile.Copy(_L("c:\\StreamExam.dat"));
RFs& fs=CCoeEnv::Static()->FsSession();
RFileReadStream fileReadStream;
User::LeaveIfError(fileReadStream.Open(fs,fnDataFile,EFileRead));
fileReadStream.PushL();
//从文件中内部化数据
TInt32 nID;
TBuf16<20> bufName;
fileReadStream >> nID;
fileReadStream >> bufName;
RDebug::Print(_L("start\n"));
RDebug::Print(_L("ID=%d Name=%S\n"),nID,&bufName);
//内部化自定义类
TUserGroup userGroup;
fileReadStream >> userGroup;
RDebug::Print(_L("GroupID=%d GroupName=%S\n"),userGroup.m_nGroupID,&(userGroup.m_bufGroupName));
//释放资源
fileReadStream.Pop();
fileReadStream.Release();
RDebug::Print(_L("end\n"));