properties檔案快速轉為yml檔案

2021-10-03 04:26:53 字數 1454 閱讀 4162

只是做了簡單的測試,沒有考慮過檔案裡有注釋的情況

思想就是先把資料讀到map中,然後再儲存到新的檔案中

public class test 

private static void copyto(file oldfile, file newfile) throws exception

//把map中資料輸出到檔案內

show(map, bufferedwriter);}}

private static void addtomap(mapmap, string s)

if (ss[0].trim().length() == 0)

string keys = ss[0].trim().split("\\.");

object o = map.get(keys[0]);

if (o == null) else

}if (keys.length == 1) else

}mapnowmap = map;

for (int i = 1; i < keys.length - 1; i++)

nowmap = (hashmap) nowmap.get(keys[i - 1]);

if (!nowmap.containskey(keys[i]))

}int last = keys.length - 1;

nowmap = (hashmap) nowmap.get(keys[last - 1]);

if (nowmap.containskey(keys[last])) else

}private static void show(mapmap, bufferedwriter bufferedwriter) throws ioexception

private static void show(mapmap, int indent, bufferedwriter bufferedwriter) throws ioexception else if (value instanceof map) else }}

public static string indentstr;

static

private static string getstr(int indent)

return indentstr[indent];

}private static void resize(int len)

} else

for (int i = indentstr == null ? 1 : indentstr.length; i < newstr.length; i++)

indentstr = newstr;}}

原檔案內容:

轉換後內容:

載入properties檔案

1 使用絕對路徑 string filename c a.properties properties p new properties inputstream input new fileinputstream filename p.load input 缺點 使用了絕對路徑,不利於專案的拷貝 頻繁...

properties 檔案讀取

假設在src根目錄下有檔案 config.properties 下面 就是讀取這個檔案的例子程式 方法1 properties prop new properties inputstream in object.class.getresourceasstream config.properties ...

Properties讀寫檔案

properties是hashtable 執行緒安全 不能以null作為key或者value properties 類表示了乙個持久的屬性集。properties 可儲存在流中或從流中載入。屬性列表中每個鍵及其對應值都是乙個字串。所儲存的資料都是字串類 properties中每個鍵及其對應處理的值都...