在j2me中载入propreties资源

本文介绍了一个Java方法,用于从资源文件中加载并解析属性文件。该方法使用DataInputStream从指定路径读取属性文件,并通过逐行解析将键值对存储到Properties对象中。此外,还提供了一个用于解析每行数据的辅助方法。
    public Properties load(String res) throws IOException
{
Properties prop = new Properties();
DataInputStream is = null;
try
{
is = new DataInputStream(this.getClass().getResourceAsStream(res));
StringBuffer sb = new StringBuffer();
for (int i = is.read(); i > 0; i = is.read())
{
char c = (char) i;
if (c == '\r' || c == '\n')
{
String line = sb.toString();
if (line != null && !line.equals(""))
{
parseLine(prop, line);
sb = new StringBuffer();
}
}
else
{
sb.append((char) i);
}
}
parseLine(prop, sb.toString());
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (IOException e) { e.printStackTrace(); }
}

}
return prop;
}

private void parseLine(Properties prop, String line)
{
if (line != null && !line.equals(""))
{
int pos = line.indexOf(":");
prop.addProperty(line.substring(0, pos), line.substring(pos + 2, line.length()));
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值