JAVA中使用Properties類帶來(lái)的好處有哪些-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)JAVA中使用Properties類帶來(lái)的好處有哪些的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比冷水灘網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式冷水灘網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋冷水灘地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。

許多開(kāi)發(fā)者抱怨C++不能像Java那樣綁定Properties類。Java的Properties類內(nèi)在包含一個(gè)文件,該文件用來(lái)讀寫(xiě)Properties類中的屬性,可以寫(xiě)成這樣形式:<名字>=<數(shù)值>(例如:ConnectToInternet=Use IE)。

使用Properties類的好處就是你可以很輕松的理解和修改它們。在本文的第一部分中,你將看到我們也可以在C++中使用Properties類。本文的第二部分將向你演示通過(guò)使用操作符>>和<<把數(shù)據(jù)保存到Properties類中是多么的容易。

現(xiàn)在介紹C++ Properties文件的結(jié)構(gòu)。該文件的每一行可以是下面三種情況中的某一種:

空行(認(rèn)為它是注釋中的一部分)

以‘?!?開(kāi)始的注釋行

‘<名字>=<數(shù)值>’行,這是給一個(gè)屬性賦值的語(yǔ)句

現(xiàn)在讓我們?cè)倏纯碢roperties類的的特點(diǎn):

注釋是持久性的(當(dāng)保存Properties類時(shí),它們不會(huì)丟失掉)。注意每一個(gè)注釋都屬于某個(gè)屬性。在‘<名字>=<數(shù)值>’行上的注釋行屬于該‘<名字>’屬性。

當(dāng)保存Properties類后,屬性仍然保留自己的位置。

它對(duì)各種字符類型都有效:char、wchar_t等等

Properties類的使用相當(dāng)簡(jiǎn)單:

save():保存屬性

has_property(strPropertyName):如果類中有該屬性則返回‘真’

string get_property(strPropertyName):返回指定的屬性(如果指定屬性不存在,則拋出例外)

set_property(strPropertyName, strPropertyValue):設(shè)置給定屬性

stringget_property_comment( strPropertyName):返回屬于指定屬性的注釋(如果指定屬性的注釋不存在,則拋出例外)

set_property_comment(strPropertyName, strPropertyComment):設(shè)置指定屬性的注釋(如果指定屬性的注釋不存在,則拋出例外)

下面是file_reader_writer類以及相應(yīng)例子的代碼。運(yùn)行它之后,請(qǐng)查看properties.txt文件。看看訪問(wèn)和修改它是多么容易的一件事。


#include exception>

#include string>

#include sstream>

#include map>

#include vector>

#include fstream>

#include algorithm>

#include functional>

//允許字符串轉(zhuǎn)化

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)' ';

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();

}

// 當(dāng)讀寫(xiě)屬性時(shí)的例外

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;

};



// 從文件中讀寫(xiě)屬性

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())

// 它是一個(gè)新的屬性

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 += ' '; }

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: " + convert_string< char_type, char>( strLine));

}

}

m_strLastComment = strComment;

}



void write_properties() const

{

if ( !m_bIsDirty)

// 無(wú)需保存

;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:

// 我們用來(lái)讀寫(xiě)的文件

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;

};



下面是用到這個(gè)類的一個(gè)例子:



#include

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

{

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

rw.set_property( "App Path", "C:Program FilesPFSjokExplorer");

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;

}

感謝各位的閱讀!關(guān)于“JAVA中使用Properties類帶來(lái)的好處有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

網(wǎng)頁(yè)標(biāo)題:JAVA中使用Properties類帶來(lái)的好處有哪些-創(chuàng)新互聯(lián)
文章分享:http://muchs.cn/article24/pssce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、靜態(tài)網(wǎng)站網(wǎng)站收錄、微信小程序移動(dòng)網(wǎng)站建設(shè)、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)