今天在项目中有一个需求是取出所有的属性 按照字母排序,实现起来不算难,小计一下,主要是用到了Collections.sort(list);这个方法排序
先是读出所有的属性,过滤后放进list里面 Collections.sort(list) 搞定
public static String getServerConfigInfo()
{
String line;
StringBuffer infos = new StringBuffer();
try
{
String serverConfigPath = getServerConfigPath();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(serverConfigPath)));
List<String> list = new ArrayList<String>();
while ((line = br.readLine()) != null)
{
int pos = line.indexOf("=");
if (pos > 0 && !line.startsWith("#"))
{
String key = line.substring(0, pos);
if (key.indexOf("password") < 0 && key.indexOf("newpasswd") < 0)
{
list.add(line);
}
}
}
Collections.sort(list);
for (String s : list)
{
infos.append(s).append("\n");
}
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return infos.toString();
}