天天看點

簡單測試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