天天看点

简单测试java - properties

import java.io.bufferedinputstream;

import

java.io.fileinputstream;

import java.io.fileoutputstream;

java.io.inputstream;

import java.util.enumeration;

java.util.properties;

public class testporperty {

 /**

     * 写入-properties文件:

     *

        conf 传递过来的content里边的字符串

  *         xmlpath是指定的路径

  * */

    public void writeproperty(string conf,string

xmlpath) throws exception{

        properties

prop = new properties();

         *

通过"<,>"符号截

         *   

     prop.setproperty(key,value)

      *       

 prop.store()写入到传递过来的xmlpath

*/

        string values[] =

conf.split("<,>");

        for (string

value : values) {

 int index = value.indexof("=");

     prop.setproperty(value.substring(0,

index),

     value.substring(index + 1));

         prop.store(new

fileoutputstream(xmlpath),

         xmlpath);

     }

    }

    /**

     * 读取:

  *     格式化-properties文件内容为字符串格式

        xmlpath是-properties文件路径

    public void fotmatxml(string

xmlpath){

        properties prop = new

properties();

        stringbuffer content =

new stringbuffer();

        string cont =

"";

        try {

         inputstream in = new

bufferedinputstream(new fileinputstream(

 xmlpath));

 prop.load(in);

 //返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键

         enumeration<?> en =

prop.propertynames();

 while (en.hasmoreelements()) {

 //如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素

         string key = (string)

en.nextelement();

     //用指定的键在此属性列表中搜索属性

         string property =

prop.getproperty(key);

     cont = key + "=" + property +"<,>";

 content.append(cont);

 string con = content.substring(0, content.length()-3);

 system.out.println(con);

        } catch

(exception e) {

 e.printstacktrace();

 }

     * 1、把字符串写入到-properties文件

* 2、格式化文件-properties为字符串

     * */

 public static void main(string args[]) throws exception {

        string conf =

fileoutputstream("devoment.properties"),

 "devoment.properties");

        stringbuffer content = new

stringbuffer();

 "devoment.properties"));

     prop.load(in);

     enumeration<?> en =

     string property =

    }  

}

上一篇: Swift