社区
Eclipse
帖子详情
Property
XT4625
2009-03-30 09:13:38
如何用property访问*.properties中的所有内容?
如:文件名:ATM.properties文件的中有内容如下:
username=a1
password=1
username=a2
password=2
username=a3
password=3
默认情况下我只能访问
最后一个
,如何将前面的内容也访问到?
...全文
73
5
打赏
收藏
Property
如何用property访问*.properties中的所有内容? 如:文件名:ATM.properties文件的中有内容如下: username=a1 password=1 username=a2 password=2 username=a3 password=3 默认情况下我只能访问最后一个,如何将前面的内容也访问到?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
5 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
quanjinzhang
2009-03-31
打赏
举报
回复
我写的一个例子,不过这里也会覆盖掉,需要你通过hashmap之类的类来存放所有值
否则:mAllProperties.setProperty(key, value);会覆盖掉以前的属性键值对。
/**
* 通过读取配置文件路径对应的配置文件,获取属性键值对
* @return
*/
private boolean getProperties(String tPropertieFilePath){
File myPropertyfile = null;
boolean returnValue = true;
try {
myPropertyfile = new File(tPropertieFilePath);
if(!myPropertyfile.exists())
{
System.out.println("没有找到属性配置文件“" + tPropertieFilePath
+ "”,请在当前目录下创建config目录,并将名为config.properties的属性配置文件存放到该目录下!");
}else if(!myPropertyfile.canRead())
{
System.out.println("无法读取设定的属性配置文件“" + tPropertieFilePath +"”,请设定属性配置文件的属性为可读!");
}
FileReader fr = null;
BufferedReader br = null;
fr = new FileReader(myPropertyfile);
br = new BufferedReader(fr);
String line = br.readLine();
int i=0;
while(line!=null){
line = line.trim();
int equalIndex = line.indexOf("=");
if(equalIndex>0 && !line.startsWith("#"))
{
String key = line.substring(0, equalIndex).trim().toLowerCase();
String value = line.substring(equalIndex+1, line.length()).trim();
if(!key.equals("") && !value.equals(""))
{
i++;
if(i==1){
mAllProperties = new Properties();
}
mAllProperties.setProperty(key, value);
}
}
line = br.readLine();
}
br.close();
fr.close();
if(mAllProperties.isEmpty())
{
System.out.println("属性配置文件“" + tPropertieFilePath + "”里没有任何属性配置信息,请重新配置相关信息!");
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
System.out.println("没有找到属性配置文件“" + tPropertieFilePath
+ "”,请在当前目录下创建config目录,并将名为initconfig.properties的属性配置文件存放到该目录下!");
returnValue = false;
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取属性配置文件“" + tPropertieFilePath + "”的内容时发生IO错误!");
returnValue = false;
}
return returnValue;
}
danielzhan
2009-03-31
打赏
举报
回复
properties的父类是hashtable,所以properties中是不能有重名的key的.
得自己写一个类似properties的class,要不就干脆把文件作为txt来读,再自己解析内容.
Study_Work_2009
2009-03-31
打赏
举报
回复
up
fireinjava
2009-03-31
打赏
举报
回复
public class Test {
private static Properties properties = new Properties();
static {
try {
properties.load(GlobalError.class.getClassLoader().getResourceAsStream("src/ATM.properties"));//这边路径自己要配下
} catch (IOException e) {
}
}
public static void main(String args[]) {
System.out.println(properties.get("username"));//用get(key)
}
}
quanjinzhang
2009-03-30
打赏
举报
回复
1、为什么必须使用一样的属性名称呢?如果不使用一样的属性名称,问题不就解决了吗?
2、自己通过java.io.*中的FileReader等去一行一行读取,肯定能都获取到所有值了。
标签系列三:spring 中
property
解释以及
property
标签里面的属性
一、
property
标签: 英文解释:Bean definitions can have zero or more properties.
Property
elements correspond to JavaBean setter methods exposed by the bean classes. Spring supports primitives, references to ot...
源码分析 There is no getter for
property
named '*' in 'class java.lang.String
There is no getter for
property
named '*' in 'class java.lang.String',此错误之所以出现,是因为mybatis在对parameterType="String"的sql语句做了限制,假如你使用
这样的条件判断时,就会出现该错误,不过今天我们来刨根问底一下。
VUE中出现 Cannot read
property
'length' of undefined 的错误
** 报错 Cannot read
property
‘length’ of undefined 时解决方案 ** 在做项目是遇到了length的报错 在做项目时遇到了length的报错。但是不影响其他东西,只是看着飘一片红感觉很难受。 查找了一下发现出现的原因 其实是因为,后台在返回数据时是需要时间的 不管多快 都是需要时间的 但是在初始化时,并没有给一个空的数组 注意 是数组形式 所...
Android 属性
property
_get/
property
_set
1、
property
_get和
property
_set使用方法
property
_get和
property
_set使用方法 一.使用方法 每个属性都有一个名称和值,他们都是字符串格式。属性被大量使用在Android系统中,用来记录系统设置或进程之间的信息交换。属性是在整个系统中全局可见的。每个进程可以get/set属性。 如下面我们可以在系统中代码设置属性或命令行设置属性,用于系统中的判断, setprop WLAN.SWITCH.FLAG 1 getprop WLAN.SWITCH.FLAG ..
“
Property
or method “***“ is not defined on the instance but referenced during render.”报错的原因及解决方案
报错问题: 在使用vue-cli运行项目的过程中,在VScode中不报错,但在浏览器调试工具中发出 [Vue warn]:
Property
or method "pic" is not defined on the instance but referenced during render. Make sure that this
property
is reactive, either in the data option, or for class-based components, by in.
Eclipse
58,446
社区成员
49,460
社区内容
发帖
与我相关
我的任务
Eclipse
Java Eclipse
复制链接
扫一扫
分享
社区描述
Java Eclipse
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章