Solve the cache problem when modifying the properties property file.

Source: Internet
Author: User

During project creation, some data may not need to be managed in the database, such as the configuration of database connections and scheduled tasks .. sometimes the data needs to be modified dynamically, but the problem occurs when the data is retrieved again after the modification. modify properties in the project, and then go to the relevant directory to view the properties file. It is found that the content has been modified, but through TaskController. class. getResourceAsStream ("/config. properties "); the data obtained does not change the previous data. the reason is :. getResourceAsStream is obtained through the cache. solution: Obtain TaskController in a real path. class. getResource ("/config. properties "). getPath (); operation properties file tool class:

Package com. lanyuan. video. util; import java. io. bufferedInputStream; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. inputStream; import java. util. iterator; import java. util. properties; import java. util. map. entry; import com. lanyuan. video. task. taskController; public class PropertiesUtils {/*** get the data of the attribute file according to the key value * @ param fileName file name (note: the file under src is loaded, if it is under a certain package. add the package name) * @ param key * @ return */public static String findPropertiesKey (String key) {try {Properties prop = getProperties (); return prop. getProperty (key) ;}catch (Exception e) {return "" ;}} public static void main (String [] args) {Properties prop = new Properties (); inputStream in = TaskController. class. getResourceAsStream ("/config. properties "); try {prop. load (in); Iterator <Entry <Object, Object> itr = prop. entrySet (). iterator (); while (itr. hasNext () {Entry <Object, Object> e = (Entry <Object, Object>) itr. next (); System. err. println (e. getKey (). toString () + "" + e. getValue (). toString ();} catch (Exception e) {}}/*** return Properties * @ param fileName file name (note: the file under src is loaded, if it is under a certain package. add the package name) * @ param * @ return */public static Properties getProperties () {Properties prop = new Properties (); String savePath = TaskController. class. getResource ("/config. properties "). getPath (); // The following method will cache the attribute file. // InputStream in = TaskController. class //. getResourceAsStream ("/config. properties "); try {InputStream in = new BufferedInputStream (new FileInputStream (savePath); prop. load (in);} catch (Exception e) {return null;} return prop ;} /*** write properties Information ** @ param key * name * @ param value */public static void modifyProperties (String key, String value) {try {// read the attribute list (key and element pair) from the input stream Properties prop = getProperties (); prop. setProperty (key, value); String path = TaskController. class. getResource ("/config. properties "). getPath (); FileOutputStream outputFile = new FileOutputStream (path); prop. store (outputFile, "modify"); outputFile. close (); outputFile. flush () ;}catch (Exception e ){}}}

 

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.