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!