Read properties configuration file under Java application __java

Source: Internet
Author: User


In Java application development, it is often necessary to read a configuration file to obtain system parameters or configuration information. Profiles can be used in XML format files, and in Java. properties files are used specifically as configuration files. In Java, class properties are used to process configuration file-related reads. The following is an example of obtaining a value based on the provided key.

public static string GetValue (String key)
 {
 	Properties P=new properties ();
 	FileInputStream fis;
 	String url = new File (""). GetAbsolutePath () + file.separator+  "config.properties"; Gets the config.properties configuration file absolute path
 	try {
 		fis = new FileInputStream (URL) located in the engineering root directory;
 		P.load (FIS);
 		Fis.close ();
 	} catch (Exception e) {
 		//TODO auto-generated catch block
 		System.out.print (' Problems with properties ');
 		E.printstacktrace ();
 	}		
 	return P.getproperty (key);		
 }

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.