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 ){}}}