用Collections.sort()实现按照首字母排序

本文介绍了一种从文件中读取并按字母顺序排序配置项的方法。通过使用Java的BufferedReader来读取文件内容,并利用Collections.sort()方法对读取到的配置项进行排序。

今天在项目中有一个需求是取出所有的属性 按照字母排序,实现起来不算难,小计一下,主要是用到了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();
	}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值