Java uses the properties file to configure dynamic information

Source: Internet
Author: User
Java Properties Class reads configuration file information when writing a program in peacetime, some parameters are often changed, and this change is not what we foresee. The properties file in Java is a configuration file, mainly used to express configuration information, file type is *.properties, format is text file, file content is format is "key = value" format, in the properties file can use "#" to make comments, The properties file is a lot of places to use in Java programming and is easy to operate.


1, get the system attributes of the JVM import java.util.Properties;

Class Proptest {
public static void Main (string[] args) {
Properties pps = System.getproperties ();
Pps.list (System.out);
}
}
2. Read the configuration file
Configuration file Config.ini
Author=zj
User=all
copyright=2006-2007 Get configuration file code
Import Java.io.FileInputStream;
Import java.util.Enumeration;
Import java.util.Properties;

Class Proptest {
public static void Main (string[] args) {
Properties Pps=new properties ();
try {
Pps.load (New FileInputStream ("Config.ini"));
Enumeration enum1 = Pps.propertynames ();
while (Enum1.hasmoreelements ()) {
String strkey = (string) enum1.nextelement ();
String strvalue = Pps.getproperty (strkey);
System.out.println (strkey + "=" + strvalue);
}
catch (Exception e) {
E.printstacktrace ();
}
}
}
3, another example of the operation of the properties file
Import Java.io.BufferedInputStream;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.util.Enumeration;
Import java.util.Properties;

public class Testmain {

Read value based on key
public static string ReadValue (String filepath,string key) {
Properties Props = new properties ();

try {
InputStream in = new Bufferedinputstream (new FileInputStream (FilePath));
Props.load (in);
String value = Props.getproperty (key);
System.out.println (Key+value);
return value;
catch (Exception e) {
E.printstacktrace ();
return null;
}
}

Read all the information for the properties
public static void ReadProperties (String filePath) {
Properties Props = new properties ();

try {
InputStream in = new Bufferedinputstream (new FileInputStream (FilePath));
Props.load (in);
Enumeration en = Props.propertynames ();
while (en

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.