Java reads the properties file and java reads properties
In actual development, we will put some path file configurations in the properties file, so we only need to write this configuration file when modifying the path, you do not need to modify the code one by one. But how can we get the values in the configuration file? In fact, this is very simple. We only need to encapsulate the following tool class:
Public class UrlUtil {private static Properties config = null; static {InputStream in = UrlUtil. class. getClassLoader (). getResourceAsStream ("url. properties "); config = new Properties (); try {config. load (in); in. close ();} catch (IOException e) {// read the url. properties error e. printStackTrace () ;}}/*** obtain the corresponding value through the key * @ param key * @ return */public static String getValue (String key) {try {String value = config. getProperty (key); return value. trim ();} catch (Exception e) {e. printStackTrace (); return null ;}/ *** read all properties information */public static void getAllProperties () {try {Enumeration en = config. propertyNames (); while (en. hasMoreElements () {String key = (String) en. nextElement (); String Property = config. getProperty (key) ;}} catch (Exception e) {e. printStackTrace (); System. err. println ("ConfigInfoError" + e. toString () ;}} public static void main (String args []) {}}
The url. properties in the Code is the configuration file of our placement path. For example, there is an image path in our url. properties: imgPath = c:/images
To obtain this value, you only need to use UrlUtil. getValue ("imgPath.