Why do we need Properties class in java?

馋奶兔 提交于 2021-02-19 01:16:30

问题


I have always wondered why I need a Properties class as I can always create a HashMap and keep the key/value pairs there. May be it reduces the amount of code to write to load/store properties file. Because otherwise we have to create BufferedReader and read files and split the String and all these. But if we are going to get our key / value pairs from sources other than file then probably it doesn't make any difference whether we are using Properties class or HashMap class. I just need confirmation whether my thinking process is correct.

Thanks


回答1:


Properties is a class that has been part of Java since Java 1.0 .... well before Map and HashMap were introduced. In fact, you will see that Properties extends the old (legacy) Hashtable class which was the precursor to HashMap.

Properties plays an important role in a significant percentage of Java applications in the form of the system properties object. It cannot be replaced in that role without introducing compatibility problems. (Even a change that introduced a second (dual) properties mechanism would be problematic ... since some code writes to the system Properties object.)

Properties has some important functionality that HashMap does not provide; i.e. the ability to load and save the properties in 2 standard human readable formats.


May be it reduces the amount of code to write to load/store properties file. Because otherwise we have to create BufferedReader and read files and split the String and all these.

Yes. And you will find that the properties file syntax is more complicated than can be parsed with split or regexes. Look at the syntax described here:

  • https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html#load-java.io.Reader-

But if we are going to get our key / value pairs from sources other than file then probably it doesn't make any difference whether we are using Properties class or HashMap class.

Again, correct. For that use-case, HashMap might even be superior. Most methods of Properties / Hashtable are synchronized. If you don't need that synchronization, it is a (small) performance penalty.




回答2:


As you've stated, the Properties class does give the ability to read/save from/to disk, but it does a lot more than just that. It can read and store from any InputStream, so you can parse Properties from a String, or read Properties from a URL, or a Socket, or what have you. It also can convert Properties to and from XML, which isn't as useful now as it was say 10 years ago, but nevertheless a cool feature. Most importantly, you don't have to write your own parser for the syntax described in the article below:

https://en.wikipedia.org/wiki/.properties

It lets you define arguments to your program outside of your program, so your program can stay generic to what it is trying to accomplish and your configuration doesn't have to be hard-coded into your app.




回答3:


Easy Maintenance: If any information is changed from the properties file, you don't need to recompile the java class. In other words, the advantage of using properties file is we can configure things which are prone to change over a period of time without need of changing anything in code.

A properties file is a simple collection of key-value pairs that can be parsed by the java.util.Properties class.

Properties files are mostly used to store configuration or localization data. They are used to externalize the data which is configurable to the application. If you put such data in your code (as a enum or class) and want to modify it, you have to build the code again. The main advantage of properties is that they are stored externally or outside the source code thus allowing you to modify them as and when required.



来源:https://stackoverflow.com/questions/37061226/why-do-we-need-properties-class-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!