Java property file properties common operation tool class, properties tool class

Source: Internet
Author: User

Java property file properties common operation tool class, properties tool class
For the java. util. Properties class, we usually only need to achieve the following three learning objectives:
1. Understand the properties file, understand its meaning, and create the properties file correctly.
2. the java. util. Properties class is used to operate the properties file.
3. Master the relative path and correctly write the relative path of a properties file.
In normal work, there are various requirements. The following is an encapsulation.

Package com. herman. util; import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream; import java. util. properties;/*** properties file operation tool class * @ author Herman. xiong * @ date 04:27:19 * @ version V3.0 * @ since jdk 1.6, tomcat 6.0 */public class PropertiesUtil {private static Properties prop = new Properties (); private static InputStream in; private sta Tic final String path = "src/com/herman/config/conf. properties ";/*** load */static {init ();}/*** initialize */private static void init () {try {in = new FileInputStream (new File (path); prop. load (in);} catch (Exception e) {e. printStackTrace () ;}}/*** disable InputStream * @ author Herman. xiong * @ date 09:41:04 */private static void close () {try {if (null! = In) in. close ();} catch (Exception e) {e. printStackTrace () ;}/ *** re-load Properties * @ author Herman. xiong * @ date 09:41:04 */public static void reload () {close (); prop. clear (); // clear all loaded key-value pairs init ();}/*** delete all key values * @ author Herman. xiong * @ date 09:42:04 */public static void removeAll () {close (); File file = new File (path); if (file. exists () file. delete (); try {if (! File. exists () new File (path ). createNewFile ();} catch (IOException e) {e. printStackTrace () ;}}/*** output all content to the console * @ author Herman. xiong * @ date 09:42:33 */public static void outList () {prop. list (System. out);} public static void main (String [] args) {System. out. println (prop. getProperty ("abc"); outList (); reload (); outList (); removeAll (); System. out. println ("reload ..... "); outList ();}}

Welcome to my blog! If you have any questions, please join the QQ group: 135430763 to learn together!

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.