Package control;
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.hasmoreelements ()) {
String key = (string) en.nextelement ();
String property = Props.getproperty (key);
System.out.println (Key+property);
}
catch (Exception e) {
E.printstacktrace ();
}
}
Write Properties Information
public static void WriteProperties (String filepath,string parametername,string parametervalue) {
Properties prop = new properties ();
try {
InputStream fis = new FileInputStream (FilePath);
Read the list of properties from the input stream (key and element pairs)
Prop.load (FIS);
Call the Hashtable method put. Use the GetProperty method to provide parallelism.
Force the requirement to use a string for the key and value of the property. The return value is the result of the Hashtable call put.
OutputStream fos = new FileOutputStream (FilePath);
Prop.setproperty (ParameterName, ParameterValue);
In a format that is suitable for loading into the Properties table by using the Load method.
Writes the list of properties in this properties table (key and element pairs) to the output stream
Prop.store (FOS, "Update" "+ parametername +" ' value ");
catch (IOException e) {
System.err.println ("Visit" +filepath+ "for updating" +parametername+ "value error");
}
}
public static void Main (string[] args) {
readvalue ("Info.properties "," url ");
writeproperties ("Info.properties", "Age", "21");
readproperties ("info.properties");
System.out.println ("OK");
}
}