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