Java Read Properties Property file

Source: Internet
Author: User
Package com.newer;


Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.net.URI;
Import java.net.URISyntaxException;
Import java.util.Properties;


/**
* Dynamically read properties File class
* @author RSun
* @Date 2012-11-14 03:18:17
*/
Public final class Readpropertiesfile {


/** the full name of the property file and reconfigure Pfile if needed **/
private static String PFILE = "Test.properties"; SRC underneath the file

The Property object variable for the/** property file **/
Private long m_lastmodifiedtime = 0;

/** the file object variable corresponding to the property file **/
Private File m_file = null;

The Property object variable for the/** property file **/
Private Properties m_props = null;


/** Unique Instance **/
private static Readpropertiesfile m_instance = new Readpropertiesfile ();




/** Private Constructor **/
Private Readpropertiesfile () {
try {
M_lastmodifiedtime = GetFile (). LastModified ();
if (M_lastmodifiedtime = = 0) {
System.err.println (PFILE + "file does not exist!");
}
M_props = new Properties ();
M_props.load (New FileInputStream (GetFile ()));


catch (URISyntaxException e) {
System.err.println (PFILE + "Incorrect file path");
E.printstacktrace ();
catch (Exception e) {
System.err.println (PFILE + "file read exception");
E.printstacktrace ();
}
}



/**
* Find classpath path to get files
* @return File Object
* @throws URISyntaxException
*/
Private File GetFile () throws URISyntaxException {
URI Fileuri = This.getclass (). getClassLoader (). GetResource (PFILE). Touri ();
M_file = new file (Fileuri);
return m_file;
}



/**
* Static Factory method
* @return Returns a single instance of Configurationread
*/
Public synchronized static Readpropertiesfile getinstance () {
return m_instance;
}



/**
* Read a specific property entry
* @param name of a property item
* @param defaultval Read failed default value
* @return
*/
public string Getconfigitem (string name, String defaultval) {
Long newtime = m_file.lastmodified ();
Check whether the property file has been modified
if (NewTime = = 0) {
Property file does not exist
if (M_lastmodifiedtime = = 0) {
System.err.println (PFILE + "file does not exist!");
} else {
System.err.println (PFILE + "file was deleted!!");
}
return defaultval;
else if (NewTime > M_lastmodifiedtime) {
M_props.clear ();
try {
M_props.load (New FileInputStream (GetFile ()));
catch (Exception e) {
System.err.println ("File re-read exception");
E.printstacktrace ();
}
}
M_lastmodifiedtime = NewTime;
String val = m_props.getproperty (name);
if (val = = null) {
return defaultval;
} else {
return Val;
}
}




/**
* Read a specific property entry
* @param the item name of the Name property item
* @return The value of the property entry (such an item exists), NULL (such an item does not exist)
*/
public string Getconfigitem (string name) {
Return Getconfigitem (Name, "");
}



/**
* Read a specific property entry
* @param name of a property item
* @return The value of the space after the Intercept
*/
public string Getconfigitemtrim (string name) {
Return Getconfigitem (Name, ""). Trim ();
}


Test
public static void Main (string[] args) {
System.out.println (Readpropertiesfile.getinstance (). Getconfigitemtrim ("AAA"));
System.out.println (Readpropertiesfile.getinstance (). Getconfigitemtrim ("BBB"));
System.out.println (Readpropertiesfile.getinstance (). Getconfigitem ("CCC", "Haha, you've been fooled.") "));
}


}




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.