Easy to manipulate properties files with JAVA (turn to others) __java

Source: Internet
Author: User

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");
   }
}

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.