[Common Java class libraries]: PropUtils attribute File class

Source: Internet
Author: User

Description of how to read and modify the properties file in Java: 1. properties getProperties (String filepath): Read the property file according to the path and return the Properties class; 2. void SaveProperties (Properties prop, String filepath): Save the property to the specified path (if the File does not exist, create a File first); 3. string getConfigValue (String key): Get the attribute of the specified Key; 4. void setConfigValue (String key, String value): sets the attribute value of the specified Key. The property file called in method 3 and 4 is the default file. The file name and path are set through CONFIG and LOCATION. The file is located under src in the project, export the jar file under the same directory (obtained by LOCATION ). The following code: [java] public class PropUtils {public static String LOCATION; public static final String CONFIG = "config. properties "; static {try {String temp = URLDecoder. decode (PropUtils. class. getProtectionDomain (). getCodeSource (). getLocation (). getFile (), "UTF-8"); LOCATION = temp. substring (1, temp. lastIndexOf ('/');} catch (UnsupportedEncodingException e) {LOCATION = "" ;}}/*** @ param args * @ thr Ows Exception */public static Properties getProperties (String filepath) throws Exception {Properties prop = new Properties (); FileInputStream FD = new FileInputStream (LOCATION + "/" + filepath); prop. load (FCM); return prop;} public static void SaveProperties (Properties prop, String filepath) throws Exception {FileOutputStream fos = new FileOutputStream (LOCATION + "/" + filepath); prop. store (fos, "@ aut Hor Isea533 "); fos. close ();} public static String getConfigValue (String key) {try {Properties properties = getProperties (CONFIG); if (properties. get (key )! = Null) {return properties. get (key ). toString () ;}} catch (Exception e) {System. out. println (e. getMessage ();} return "";} public static void setConfigValue (String key, String value) {try {Properties properties = getProperties (CONFIG); properties. setProperty (key, value); SaveProperties (properties, CONFIG);} catch (Exception e) {System. out. println (e. getMessage ());}}}

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.