动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
电脑书籍下载:程序设计书籍 | 数据库教程书籍 | 平面与多媒体书籍 | 网络通讯书籍 | 系统管理书籍 | 网络安全书籍 | 认证考试书籍
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ Java教程 ]的信息



本月文章推荐
.SUN宣布发布更多免费软件.
.java.util包学习笔记一.
.如何学习Java呢.
.Java程序员的存储过程.
.Jakarta Struts应用的七个经验(.
.J2SE综合:对java.util的总结 七.
.从JAR文件中读取文件.
.NetBeans IDE 4.1 快速入门指南(.
.Java 学习之深入研究类的各种属性.
.The Xlet Application Model.
.EJB设计模式(1).
.JAVA的动态编译和静态编译.
.用Java事件处理机制实现录制回放.
.初学者入门 JAVA 的 XML 编程实例.
.学习java安全技术前的准备.
.用内嵌类减少JAVA程序设计中的混.
.使用设计模式改善程序结构(1).
.Sun最新发布的Java EE 6的特点详.
.Java Applet 入门第三天.
.使用Hibernate进行对象的关系映射.

使用Properties类带来的好处

发表日期:2008-1-5 |


 

许多开发者抱怨C++不能像Java那样绑定Properties类。Java的Properties类内在包含一个文件,该文件用来读写Properties类中的属性,可以写成这样形式:<名字>=<数值>(例如:ConnectToInternet=Use IE)。

使用Properties类的好处就是你可以很轻松的理解和修改它们。在本文的第一部分中,你将看到我们也可以在C++中使用Properties类。本文的第二部分将向你演示通过使用操作符>>和<<把数据保存到Properties类中是多么的轻易。

现在介绍C++ Properties文件的结构。该文件的每一行可以是下面三种情况中的某一种:

空行(认为它是注释中的一部分)

以‘#’ 开始的注释行

‘<名字>=<数值>’行,这是给一个属性赋值的语句

现在让我们再看看Properties类的的特点:

注释是持久性的(当保存Properties类时,它们不会丢失掉)。注重每一个注释都属于某个属性。在‘<名字>=<数值>’行上的注释行属于该‘<名字>’属性。

当保存Properties类后,属性仍然保留自己的位置。

它对各种字符类型都有效:char、wchar_t等等

Properties类的使用相当简单:

save():保存属性

has_property(strPropertyName):假如类中有该属性则返回‘真’

string get_property(strPropertyName):返回指定的属性(假如指定属性不存在,则抛出例外)

set_property(strPropertyName, strPropertyValue):设置给定属性

stringget_property_comment( strPropertyName):返回属于指定属性的注释(假如指定属性的注释不存在,则抛出例外)

set_property_comment(strPropertyName, strPropertyComment):设置指定属性的注释(假如指定属性的注释不存在,则抛出例外)

下面是file_reader_writer类以及相应例子的代码。运行它之后,请查看properties.txt文件。看看访问和修改它是多么轻易的一件事。

#include exception>

#include string>

#include sstream>

#include map>

#include vector>

#include fstream>

#include algorithm>

#include functional>

//答应字符串转化

template< class FromCharType, class ToCharType>

inline std::basic_string< ToCharType> convert_string( const std::basic_string< FromCharType> & strSource)

{

std::basic_string< ToCharType> strDest;

int nSourceLen = strSource.length();

strDest.resize( nSourceLen);

for ( int idxChar = 0; idxChar < nSourceLen; idxChar++)

{ strDest[ idxChar] = ( ToCharType)strSource[ idxChar]; }

return strDest;

}

// 用于trim_spaces;

template< class CharType>

strUCt is_char_in_str : public std::binary_function< CharType, std::basic_string< CharType>, bool>

{

bool operator()( CharType ch, const std::basic_string< CharType> & strSource) const

{ return (strSource.find( ch) != std::basic_string< CharType>::npos); }

};

//消除字符串中的空格

template< class CharType>

std::basic_string< CharType> trim_spaces( const std::basic_string< CharType> & strSource)

{

std::basic_string< CharType> strSpaces; strSpaces += ( CharType)' '; strSpaces += ( CharType)'\t';

typedef std::basic_string< CharType> string_type;

string_type::const_iterator

itFirst = std::find_if( strSource.begin(), strSource.end(),

std::not1( std::bind2nd( is_char_in_str< CharType>(), strSpaces)));

string_type::const_reverse_iterator

ritLast = std::find_if( strSource.rbegin(), strSource.rend(),

std::not1( std::bind2nd( is_char_in_str< CharType>(), strSpaces)));

string_type::const_iterator itLast = &*ritLast;

if ( itFirst <= itLast)

if ( itFirst != strSource.end())
return string_type( itFirst, itLast + 1);

return string_type();

}

// 当读写属性时的例外

class properties_exception : public std::exception

{

public:

properties_exception( const std::string & str) : m_strDescription( str) {}

const char * what() const { return m_strDescription.c_str(); }

private:

std::string m_strDescription;

};

// 从文件中读写属性

template< class CharType>

class file_reader_writer

{

typedef std::basic_string< CharType> string_type;

public:

// ... needed within the basic_properties!

typedef CharType char_type;

public:

file_reader_writer( const char * strFileName)

: m_bIsDirty( false), m_strFileName( strFileName) { read_properties(); }

~file_reader_writer() { save(); }

void save()

{ write_properties(); }

bool has_property( const string_type & strPropertyName) const

{

PropertiesCollection::const_iterator itFound = m_collProperties.find( strPropertyName);

return ( itFound != m_collProperties.end());

}

const string_type & get_property( const string_type & strPropertyName) const

{

PropertiesCollection::const_iterator itFound = m_collProperties.find( strPropertyName);

if ( itFound != m_collProperties.end())

return itFound->second.m_strValue;

else

throw properties_exception(

"Cound not get property value for '" + convert_string< char_type, char>( strPropertyName) +

"', since this property does not exist.");

}

void set_property( const string_type & strProperty, const string_type & strPropertyValue)

{

PropertiesCollection::iterator itFound = m_collProperties.find( strProperty);

if ( itFound == m_collProperties.end())

// 它是一个新的属性

m_aProperties.push_back( strProperty);

m_collProperties[ strProperty].m_strValue = strPropertyValue;

m_bIsDirty = true;

}

const string_type & get_property_comment( const string_type & strPropertyName) const

{

PropertiesCollection::const_iterator itFound = m_collProperties.find( strPropertyName);

if ( itFound != m_collProperties.end())

return itFound->second.m_strComment;

else

throw properties_exception(

"Cound not get property comment for '" + convert_string< char_type, char>( strPropertyName) +

"', since this property does not exist.");

}

void set_property_comment( const string_type & strPropertyName, const string_type & strPropertyComment)

{

PropertiesCollection::iterator itFound = m_collProperties.find( strPropertyName);

if ( itFound != m_collProperties.end())

itFound->second.m_strComment = strPropertyComment;

else

throw properties_exception(

"Cound not set property comment for '" + convert_string< char_type, char>( strPropertyName) +

"', since this property does not exist.");

m_bIsDirty = true;

}

private:

static const char_type get_delimeter() { return '='; }

static const char_type get_comment() { return '#'; }

void read_properties()

{

const char DELIMETER = get_delimeter();

const char COMMENT = get_comment();

std::basic_ifstream< char_type> streamIn( m_strFileName.c_str());

string_type strLine;

string_type strComment;

while ( std::getline( streamIn, strLine))

{

strLine = trim_spaces( strLine);
bool bIsComment = strLine.empty() ( strLine[ 0] == COMMENT);

if ( bIsComment)

{ strComment += strLine; strComment += '\n'; }

else

{

int idxDelimeter = strLine.find( DELIMETER);

if ( idxDelimeter != string_type::npos)

{

string_type strPropertyName = strLine.substr( 0, idxDelimeter);

string_type strPropertyValue = strLine.substr( idxDelimeter + 1);

strPropertyName = trim_spaces( strPropertyName);

strPropertyValue = trim_spaces( strPropertyValue);

m_collProperties.insert(

std::make_pair( strPropertyName, OneProperty( strPropertyValue, strComment)));

m_aProperties.push_back( strPropertyName);

strComment.erase();

}

else

throw properties_exception(

"While reading from file '" + m_strFileName +

"', we encountered an invalid line: \n" + convert_string< char_type, char>( strLine));

}
}

m_strLastComment = strComment;

}

void write_properties() const

{

if ( !m_bIsDirty)

// 无需保存

;return;

const char DELIMETER = get_delimeter();

std::basic_ofstream< char_type> streamOut( m_strFileName.c_str());

PropertiesArray::const_iterator

itFirst = m_aProperties.begin(), itLast = m_aProperties.end();

while ( itFirst != itLast)

{

const string_type & strPropertyName = *itFirst;

const OneProperty & property = m_collProperties.find( strPropertyName)->second;

write_property_comment( streamOut, property.m_strComment);

string_type strToWrite = strPropertyName;

strToWrite += ' '; strToWrite += DELIMETER; strToWrite += ' ';

streamOut strToWrite << property.m_strValue << std::endl;

++itFirst;

}

write_property_comment( streamOut, m_strLastComment);

m_bIsDirty = false;

}

void write_property_comment( std::basic_ofstream< char_type> & streamOut, const string_type & strComment) const

{

const char COMMENT = get_comment();

std::basic_stringstream< char_type> streamComment( strComment);

string_type strLine;

while ( std::getline( streamComment, strLine))

{

if ( !strLine.empty())

if ( strLine[ 0] == COMMENT)

streamOut << strLine << std::endl;

else

{

string_type strPrefix;

strPrefix += COMMENT; strPrefix += ' ';

streamOut << strPrefix << strLine << std::endl;

}

else

streamOut << std::endl;

}
}

private:

// 我们用来读写的文件

std::string m_strFileName;

//假如自上次保存后属性又有修改则赋值为“真”

mutable bool m_bIsDirty;

struct OneProperty

{

OneProperty() {}

OneProperty( const string_type & strValue, const string_type & strComment)

: m_strValue( strValue), m_strComment( strComment) {}

string_type m_strValue;

string_type m_strComment;

};

// 属性

typedef std::map< string_type, OneProperty> PropertiesCollection;

PropertiesCollection m_collProperties;

// ……确保我们是按同样的次序保存属性

// 读属性

typedef std::vector< string_type> PropertiesArray;

PropertiesArray m_aProperties;

// 读取所有属性后的注释

string_type m_strLastComment;

};

下面是用到这个类的一个例子:

#include

int main(int argc, char* argv[])

{

file_reader_writer< char> rw( "properties.txt");

rw.set_property( "App Path", "C:\Program Files\PFS\jokEXPlorer");

rw.set_property_comment( "App Path", "where are we installed?");

rw.set_property( "Version", "4.0.0.1");

rw.set_property_comment( "Version", "What's our version?");

rw.set_property( "Run On Startup", "1");

rw.set_property_comment( "Run On Startup", "are we run, when the computer starts?");

rw.set_property( "Automatic Logoff Minutes", "60");

rw.set_property_comment( "Automatic Logoff Minutes", "when should we deconnect from the server?");

rw.set_property( "Connect To Internet", "Use IE");

rw.set_property_comment( "Connect To Internet", "how are we to connect to the Internet?");

std::cout << "This is how we connect to Internet: " << rw.get_property( "Connect To Internet") << std::endl;

return 0;

}


上一篇:让XDoclet为你写存根类 人气:359
下一篇:浅析J2EE、J2SE和J2ME 人气:414
浏览全部Java的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-7-24 Sablog-X v2.0 预览版
2008-7-24 帝国备份王EmpireBak 2008 正式版
2008-7-24 网趣网上购物系统时尚版 v8.2
2008-7-24 纵横B2B电子商务系统XYECS!B2B v
2008-7-24 e路小说小偷 v1.2.0723
2008-7-24 凌风美女图片站程序 v2.2
2008-7-24 TOM15电影收索程序
2008-7-24 清风信息自动采集生成系统 v1.0
2008-7-24 QQ邮箱编辑器 v1.0 (小小菜刀ASP
2008-7-19 UltraEdit 简体中文增强版 14.10
2008-7-19 CentOS 5.2 i386 LiveCD
2008-7-19 Snapture多功能相机 v1.4
2008-7-19 iAcces中文输入法 v1.0Build016
2008-7-19 Cookbook烹饪秘籍 v2.5
2008-7-19 苹果专用DVD转换工具 v1.1.59汉化
2008-7-19 Modem修复软件ZiPhone修改版04.0
2008-7-19 AgileMessenger即时通讯工具美化
2008-7-19 Sketches画图软件 v0.7b6破解版


  发表评论
姓 名: 验证码:
内 容:
[ 汉字翻译拼音 ] [ 广告代码 ] [ 符号对照表 ] [ 进制转换 ] [ 经典小工具 ] [ 个税计算 ] [ 汉字简繁转换 ] [ 普通单位换算 ] [ 公制单位换算 ]
[ 生辰老黄历 ] [ 国内电话区号 ] [ 国家代码与域名缩写 ] [ 文字加密解密 ] [ 健康查询 ] [ 万年历 ] [ 手机号码查询 ] [ ip搜索 ] [ Google PR查询 ]
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵