Java reads the. properties file and java reads the. properties file.

Source: Internet
Author: User

Java reads the. properties file and java reads the. properties file.

During web development, some configurations should be saved to the properties file. This chapter provides a tool class to facilitate reading the properties file.

Case:

1: config. properties File

name=\u843D\u82B1\u6709\u610Fstr=\u6D41\u6C34\u65E0\u60C5

2: PropertiesUtil class

Package com. gcs. util; import java. io. bufferedInputStream; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream; import java. util. properties; public class PropertiesUtil {private static String default_properties = "file name. properties "; private static Properties prop; static {prop = new Properties (); try {InputStream is = new BufferedInputStream (new FileInputStream (getPath () + default_properties); prop. load (is);} catch (Exception e) {e. printStackTrace () ;}} public static String getProperty (String key) {return prop. getProperty (key);} public static String getProperty (String key, String defaultValue) {String value = prop. getProperty (key); if (value = null) return defaultValue; return value;} public static boolean getBooleanProperty (String name, boolean defaultValue) {String value = prop. getProperty (name); if (value = null) return defaultValue; return (new Boolean (value )). booleanValue ();} public static int getIntProperty (String name) {return getIntProperty (name, 0);} public static int getIntProperty (String name, int defaultValue) {String value = prop. getProperty (name); if (value = null) return defaultValue; return (new Integer (value )). intValue ();} public static String getPath () {return Thread. currentThread (). getContextClassLoader (). getResource (""). getPath ();}/*** read the value in the specified properties * @ param properties file name * @ param name attribute to be read * @ return */private String readProper (String properties, string name) {InputStream inputStream = this. getClass (). getClassLoader (). getResourceAsStream (properties); Properties p = new Properties (); try {p. load (inputStream);} catch (IOException e1) {e1.printStackTrace ();} return p. getProperty (name);} public static void main (String [] args) {PropertiesUtil propertiesUtil = new PropertiesUtil (); String name = PropertiesUtil. getProperty ("name"); String str = propertiesUtil. readProper ("config. properties "," str "); System. out. println ("name =" + name + "," + "str =" + str );}}

 

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.