使用Properties类带来的好处

dbinfo.properties 韩顺平老师细说servlet课程中用到的 dbinfo.properties配置文件 立即下载
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> Monday, February 24 2003 3:18 PM 许多开发者抱怨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; }
DEM高程数据的获取、合并与坐标转换 DEM高程数据的获取、合并与坐标转换一、DEM数据的获取二、数据合并三、投影转换方法一四、投影转换方法二 今天用DEM数据做了一个坡度分析,数据使用过程中涉及到投影坐标转换,由于不是很熟悉所以费了一番功夫。经过一番折腾总结出两个方法,方法一自认为比较便捷的方法,即通过BIGMAP软件中工具列表中的投影转换功能进行。方法二为传统方法,即在Arcgis中用投影栅格工具进行转换。 一、DEM数据的获取 在地理空间数据云网站下载DEM数据(精度包括30米和90米),数据格式为tif格式或img格式,坐标为WGS_1 阅读详情

相关推荐

zedboard(5)spi轮询和中断

一个SPI可以有几个从设备 在程序中使用 s32 XSpiPs_SetSlaveSelect(XSpiPs *InstancePtr, u8 SlaveSel) 选择。其实就是吧对应设备的ss拉低。 备注:只选一个ss,可以不用指定。(感觉,还没测试) 这是xspips.c中定义的函数,发送和接收有 XSpiPs_Transfer 和 XSpiPs_PolledTransfer 两个函数,分别采用中断和轮询模式。    一、轮询模式 米联客参考代码,SpiPs_Init中部分和官方例程流程一样,Spi

Kiss丶小坏 5890

Properties的简介及使用

1.Properties继承于Hashtable,表示了一个持久的属性集,可保存在流中或从流中加载。属性列表中,每个键及其对应值都是一个字符串。 2.Properties有一个特殊的作用,专门用来加载xxx.properties配置文件。 3.构造方法: Properties() 创建一个无默认值的空属性列表。 Properties(Propertiesdefault...

RJSong 2333

别再一个个BAPI调了!用CVI_EI_INBOUND_MAIN一次性搞定SAP BP主数据(附完整ABAP代码)

本文详细介绍了如何使用SAP标准函数CVI_EI_INBOUND_MAIN高效维护业务伙伴(BP)主数据,替代传统的多BAPI调用方式。通过完整ABAP代码示例,展示了如何一站式处理供应商、客户、公司代码等所有视图的创建与更新,提升数据一致性和开发效率。

weixin_29204205的博客 295

关于JDBC连接数据库

用户输入登录例程(此为传智播客学习视频中对应的源码,仅供学习): ...\src 下面的  dbinfo.properties文件,用于保存连接数据库需要的用户名、密码、注册的数据库驱动。只所以使用此文件来保存这些信息,是为了在更换数据库时,不需要再在代码中进行修改,直接修改此文件中对应的内容即可,可移植性高。 driverClass=com.mysql.jdbc.Driver url=jd

zhangshuaicool的博客 264

分布式爬虫架构实战:Scrapy+RabbitMQ+浏览器池,百万级数据抓取成本直降60%(附架构图+源码)

任务调度瓶颈:Redis 作为消息队列,不支持复杂任务优先级,高并发下容易消息堆积,且单点故障会导致全量任务丢失;资源浪费严重:每个爬虫节点每次爬取动态页面都启动新浏览器,启动耗时3-5秒,CPU 占用率高达80%+,单机最多同时跑3个浏览器;扩展性差:新增节点需要手动同步任务队列,节点忙闲不均,无法根据任务量动态分配资源。# 从settings读取RabbitMQ配置# 注册信号:爬虫关闭时关闭RabbitMQ连接"""启动时建立RabbitMQ连接"""

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1618

Panda3d 场景管理

许多简单的 3D 引擎维护一个 3D 模型列表,然后在每一帧对这些模型进行渲染。在这些简单的引擎中,必须分配一个3D模型(或从磁盘加载它),然后将其插入到要渲染的模型列表中。模型在插入到列表中之前对渲染器不“可见”。 Panda3D稍微复杂一些。它不是维护要呈现的对象列表,而是维护要呈现的对象树。对象在插入树之前对渲染器不可见。

u014077947的博客 1383

黑马Java学习第十一天配置文件(properties、XML)、日志、多线程(一)

配置文件、日志、多线程的创建

HuanLe.的博客 1320

R语言深度学习实战:基于YOLO的目标检测

目标检测是计算机视觉领域的一项任务,旨在识别图像或视频中的物体,并确定它们的位置。与图像分类不同,目标检测要求模型能够识别多个物体,并为每个物体提供边界框(bounding box)的位置信息。这种任务在众多应用中都具有重要价值,例如自动驾驶、安防监控、物体跟踪等。YOLO(You Only Look Once)是一种非常流行的目标检测算法,它在速度和准确性之间取得了良好的平衡。与传统的目标检测方法不同,YOLO将目标检测任务视为回归问题,直接从输入图像中预测目标的类别和位置信息。

YOLO 330

Properties类的使用

Properties的使用,配置文件对象使用

这是我的笔记本,记录学习历程,分享学习经验,菜鸡一枚,感谢支持! 1155

STM32开发 -- GPS模块开发详解

一、了解硬件 我使用的GPS模块型号为UBX-M8030 参看:UBX-M8030 系列 查看一下它的特性: 多用途 GNSS 芯片,提供三种产品等级 最多可并发接收 3 个 GNSS(GPS、伽利略、GLONASS、北斗) 行业领先的 -167 dBm 导航灵敏度 业界最低电流消耗 在城市峡谷中具有绝佳的定位精度 安全性和完整性保护 支持所有的卫星增强系统 车载级芯片的工作...

不积跬步,无以至千里 3万+

Linux 下 OpenClaw 安装全记录:从零到一的保姆级图文教程

本文主要介绍了Linux 下 OpenClaw 安装全记录:从零到一的保姆级图文教程,希望能对学习使用OpenClaw 的同学们有所帮助。 文章目录 1. 背景 2. 实战教程 2.1 步骤一 安装Node.js 2.2 步骤二:安装git 2.3 步骤三:安装并配置OpenClaw 2.4 步骤四:配置OpenClaw 2.5 步骤五:配置模型 2.6 步骤六:对话 3. 后续使用步骤 4. 报错解决方案 5. OpenClaw的卸载

weixin_43178406的博客 10万+

java编程中Properties类的具体作用和使用

如果不熟悉 java.util.Properties类,那么现在告诉您它是用来在一个文件中存储键-值对的,其中键和值是用等号分隔的。(如清单 1 所示)。最近更新的java.util.Properties 类现在提供了一种为程序装载和存储设置的更容易的方法: loadFromXML(InputStreamis) 和 storeToXML(OutputStream os, String co

Golden_lion的博客 8000

YOLOv11涨点改进 | 全网创新首发、主干改进篇 | CVPR 2025 | RAVLT主干让 YOLOv11 更加强大!通过增强线性注意力机制,特别在小目标和复杂背景的检测上表现突出

本文提出RAVLT主干网络,通过改进YOLOv11的空间特征建模能力,显著提升小物体和复杂背景检测性能。RAVLT采用秩增强线性注意力(RALA)机制,在保持线性计算复杂度的同时解决传统线性注意力低秩问题。RALA通过增强KV缓冲区和输出特征矩阵的秩,提高特征多样性和建模能力。实验表明,RAVLT在ImageNet-1k上达到84.4% Top-1准确率,仅需2600万参数和4.6GFLOPs,性能优于现有线性注意力模型。文章详细介绍了RAVLT模块结构、实现原理及在YOLOv11中的集成方法,包含四种主干

Ai缝合怪、助力小伙伴高效发论文 639

JAVA基础:使用Properties类带来的好处(转)

JAVA基础:使用Properties类带来的好处(转)[@more@]许多开发者抱怨C++不能像Java那样绑定Properties类。Java的Properties类内在包含一个文件,该文件用来读写Properties类中的...

308

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Java-Properties类和properties文件详解

Java开发中配置文件是存储程序参数的常用方式,而properties文件因其简洁的键值对格式,成为最常用的配置文件类型之一,与之配套的java.util.Properties类,则是操作properties文件的核心工具。本文我将详细讲解properties配置文件的格式规范、Properties类的常用方法,以及在实际开发中的读写操作和最佳实践。

aa_hdkf_vg的博客 1763

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Properties类给你带来一点小帮助

Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。 一个属性列表可包含另一个属性列表作为它的“默认值”;如果未能在原有的属性列表中搜索到属性键,则搜索第二个属性列表。

ricky73999的博客 504

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

java.util.Properties类操作properties文件

文章目录1 Properties类1.1 认识properties文件1.2 了解Properties类1.3 properties文件与Properties类的关系1.4 使用Properties类1.4.1 操作properties1.4.2 操作XML文件1.4.3 java读取properties文件的九种方法 1 Properties类 由于java.util.Properties类也是属于继承Map的,所以就归类到集合这章了 1.1 认识properties文件 了解并认识properties文

上善若泪 3128

Java中Properties类使用

一、Properties简介: Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的 配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样 做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像Python支持的配 置文件是.ini文件,同样,它也有自己读取配置文件的类Conf...

zw1_csdn的博客 1780

Properties类的使用方法

java.util.Properties类  用于读取.properties文本文件的类 properties文件是一个纯文本文件,里面定义的内容格式有要求, 必须是key=value的形式,并且以行为单位, 一行只记录一条数据,下图是一个properties的书写案例 Properties类可以方便的读取properties文件,并将内容  以类似HashMap的形式进行读取。注

低头低啊低的博客 869

java.util.properties 类的使用

Properties 基本知识 如果不熟悉 java.util.Properties 类,那么现在告诉您它是用来在一个文件中存储键-值对的,其中键和值是用等号分隔的,如清单 1 所示。 清单 1. 一组属性示例 foo=bar fu=baz 将清单 1 装载到 Properties 对象中后,您就可以找到两个键( foo 和 fu )和两个值( foo 的 bar 和

Cece_2012的专栏 1万+

java properties 类_Java中Properties 类的详细使用

我把你的头像,设置成我的名字,此刻你便与我同在。我把你的名字,写进我的代码里面,以后,我的世界便存在着你。一.Properties 类Properties 类位于 java.util.Properties ,是Java 语言的配置文件所使用的类, Xxx.properties 为Java 语言常见的配置文件,如数据库的配置 jdbc.properties, 系统参数配置 system.proper...

weixin_35735398的博客 465

Java Properties类使用详解

概述 Properties 继承于 Hashtable。表示一个持久的属性集,属性列表以key-value的形式存在,key和value都是字符串。 Properties 类被许多Java类使用。例如,在获取环境变量时它就作为System.getProperties()方法的返回值。 我们在很多需要避免硬编码的应用场景下需要使用properties文件来加载程序需要的配置信息,比如JDBC、M...

_夜渐凉 1200

Properties 类的使用

Properties 类已不是新东西了,它在 Java 编程的早期就有了,并且几乎没有什么变化。J2SE 的 Tiger 版本增强了这个类,不仅可以用它在单独一行中指定用等号分隔的多个键-值对,还可以用XML 文件装载和保存这些键-值对。在 驯服 Tiger的这一期文章中,John Zukowski 展示了如何驾驭这匹新一代的“役马”。请在本文对应的讨论论坛上与作者及其他读者分享您对本文的想法(您

dainiao01的专栏 849
上一篇: Sun CEO麦克尼利:我给你选择的自由
下一篇: 操纵java数组
java169
博客等级 码龄18年 27粉丝 2568原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值