Configuration parameters for your Java program (use of properties)

Source: Internet
Author: User

When we write Java programs, many times the parameters of the running program need to be changed dynamically.

A series of parameters when testing, a series of parameters at run time

Or the database address also needs a set of parameters to facilitate future dynamic deployment

The initialization of these variables, we can write a small demo when it is completely dead in the Java file

But when a program needs to be published or deployed locally, these parameters need to be removed from the program code.

We have a variety of ways to store parameters, such as databases, XML files or directly txt files

Now introduce a simple and convenient way to read parameters using Java

. properties file, we are not unfamiliar, many excellent frameworks can see its existence, such as Hibernate

In the src file directory, create a new suffix. properties file, open it with any text editor, you can use key-value pairs to set your program's operating parameters

Similar to this

#wangqun Fd60e46db0dc119cfea740c3375fd7c4#toaccountid=Fd60e46db0dc119cfea740c3375fd7c4#huangshi 1645a78135328c4btoaccountid=1645a78135328c4b#tangwei 6c0f7514f4bd0016#shixiaoping 98f30bad9e6789af#toaccountid=6c0f7514f4bd0016#toaccountid=6c0f7514f4bd0016,1645a78135328c4b,98f30bad9e6789af#cron=0 32 8-11,14-20/1 * *?Cron=0/10 * * * *?FileName=d\:/\u59dc\u5830\u5468\u79ef\u5206\u7ba1\u63a7\u53ca\u53cc\u767e\u5146\u8bad\u7ec3\u84250830.xlsxsheetname =\u6c47\u603bfromindex=0,1Toindex=48,12#fromIndex=1,4#toIndex=13,12Dirpath=c:/apache-tomcat-8.0.28/webapps/test/URLPath=http://61.132.43.176:8081/test/Database_host=132.240.9.36Database_port=1521Database_user=Jydatabase_name=yxdbdatabase_pwd=ovqwu8qsm4cwktezdnjtxg==Sql_str=select *From HS_BB_RBB R ORDER by R.nomodify_sheet=Sheet1modify_from=1,0Time_cell=1,4

Notice the points:

One, with the # notation, you can enter a number of configuration may, the runtime dynamic Comment or open, more convenient

Second, the. properties are stored in plaintext, so sensitive characters need to be encrypted, such as data passwords

Third, encountered in Chinese, is a tricky problem, the simplest way is to use the MyEclipse properties file editor to write, will automatically transfer the Chinese code

Four, all parameters can only be stored as a string, as for the type conversion, please parse the completion in Java

Five, for read-only non-writable parameter configuration, if the program needs to modify these environmental parameters during the process, it is recommended to consider the database read and write methods, rather than the properties

The following is a Java program, the Mian function can be read from the beginning of this properties file

 Public Static voidMain (string[] args)throwsexception{//read the properties configuration fileProperties prop=NewProperties (); Prop.load (Quartzdemo.class. getResourceAsStream ("/set.properties"));String Cron=prop.getproperty ("cron", "0/10 * 8-7 * *?"));String FileName=prop.getproperty ("FileName", "d:/2014 January campaign report 140116.xlsx"); String SheetName=prop.getproperty ("SheetName", "Branch View"); String Dirpath=prop.getproperty ("Dirpath", "c:/apache-tomcat-8.0.28/webapps/test/"); String URLPath=prop.getproperty ("URLPath", "http://61.132.43.176:8081/test/"); String Fromindexstr=prop.getproperty ("FromIndex", "0,0"); String Toindexstr=prop.getproperty ("Toindex", "17,20"); String Database_host=prop.getproperty ("Database_host", "" "); String Database_port=prop.getproperty ("Database_port", "" "); String Database_user=prop.getproperty ("Database_user", "" "); String database_name=prop.getproperty ("database_name", "" "); String database_pwd=deshelper.decrypt (Prop.getproperty ("Database_pwd", ""), "Newflypig"); String Sql_str=prop.getproperty ("Sql_str", "" "); String Modify_sheet=prop.getproperty ("Modify_sheet", "" "); String Modify_fromstr=prop.getproperty ("Modify_from", "0,0"); String[] Splitstr=fromindexstr.split (","); int[] Fromindex={integer.parseint (Splitstr[0]), Integer.parseint (splitstr[1])}; Splitstr=toindexstr.split (","); int[] Toindex={integer.parseint (Splitstr[0]), Integer.parseint (splitstr[1])}; Splitstr=modify_fromstr.split (","); int[] Modify_from={integer.parseint (Splitstr[0]), Integer.parseint (splitstr[1])};}

The Properties class provides a very convenient way to read the properties file, and also includes some default configuration, and again it is important to note that you can read only string, and if you encounter other data types, you must know how to convert these types with string types.

Configuration parameters for your Java program (use of properties)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.