Java reads the properties file under the project root path and any disk location

Source: Internet
Author: User
Tags getmessage

1. It is more common to read the properties file under the project root path.

The specific code is as follows:

package Com.xuanen.util;import Java.util.properties;import Org.springframework.core.io.classpathresource;import org.springframework.core.io.support.PropertiesLoaderUtils; public class Propertyutil {private static Properties propertie;private static Logger Logger = Logger.getlogger (Propertyut Il.class);/** * Read configuration file */public static Properties init () {if (Propertie = = null) {Propertie            = new Properties (); try {propertie = propertiesloaderutils.loadproperties (New Classpathresource ("Common.properties"            ));            } catch (Exception ex) {Logger.error (Ex.getmessage ());    }} return propertie;        }/** * Get SOLR config path */public static String Getdateconfigxmlpath () {propertie = init ();        String Path = Propertie.getproperty ("Dateconfigpath");    return path; }}

2. It is uncommon to read properties files anywhere on the disk, but also to master them. The code is as follows

public class getsolrcreatedate{    //Get configuration file for Dataimport.properties index creation time
Here you can replace the properties file location anywhere in the disk for example: e:/dataimport.properties

private static String Path = Propertyutil.getdateconfigxmlpath (). Replace ("Data-config.xml", "") + "Dataimport.properti Es "; private static Properties Propertie; private static Logger Logger = Logger.getlogger (Propertyutil.class); /** * Read Config file */public static Properties init () {if (Propertie = = null) {Properti E = new Properties (); try {if (Fileutils.isexcite (path)) {Propertie = PROPERTIESLOADERUTILS.LOADPR Operties (new Pathresource (path)); }} catch (Exception ex) {Logger.error (Ex.getmessage ()); }} return propertie; }//Get index changed by table name public static string Getcreatedatebytablename (String tableName) {Propertie = init ( ); String data = Propertie.getproperty (tablename+ ". Last_index_time"); if (Stringutils.isnotblank (data)) {RetuRN data; } return null; }}

  

Java reads the properties file under the project root path and any disk location

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.