如何把properties中的文件key和value全部读出来?
3个回答
展开全部
这是根据key读出value值的(key必须先知道):
Properties props = new Properties();
try {
props.load(ConnectionUtils.class.getClassLoader()
.getResourceAsStream("properties文件"));
} catch (IOException e) {
e.printStackTrace();
}
if (props != null) {
url = props.getProperty("url");
driver = props.getProperty("driver");
username = props.getProperty("username");
password = props.getProperty("password");
}
另外不知道key的话这样写:
new BufferedReader(new FileInputStream("properties文件")).readLine()一行一行读取,再处理下是Ok了
Properties props = new Properties();
try {
props.load(ConnectionUtils.class.getClassLoader()
.getResourceAsStream("properties文件"));
} catch (IOException e) {
e.printStackTrace();
}
if (props != null) {
url = props.getProperty("url");
driver = props.getProperty("driver");
username = props.getProperty("username");
password = props.getProperty("password");
}
另外不知道key的话这样写:
new BufferedReader(new FileInputStream("properties文件")).readLine()一行一行读取,再处理下是Ok了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("c:/log4j.properties");
prop.load(fis);
prop.list(System.out);
Object[] objs = prop.keySet().toArray();
for(int i=0;i<objs.length;i++){
System.out.println("key:"+objs[i]+",value:"+prop.get(objs[i]));
}
FileInputStream fis = new FileInputStream("c:/log4j.properties");
prop.load(fis);
prop.list(System.out);
Object[] objs = prop.keySet().toArray();
for(int i=0;i<objs.length;i++){
System.out.println("key:"+objs[i]+",value:"+prop.get(objs[i]));
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Config {
//Properties继承于HashMap key:value都是String类型
private Properties table=new Properties();
public Config(String file){
try{
table.load(new FileInputStream(file));
}catch (IOException e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
public int getInt(String key){
return Integer.parseInt(table.getProperty(key));
}
public String getString(String key){
return table.getProperty(key);
}
public double getDouble(String key){
return Double.parseDouble(table.getProperty(key));
}
}
//Properties继承于HashMap key:value都是String类型
private Properties table=new Properties();
public Config(String file){
try{
table.load(new FileInputStream(file));
}catch (IOException e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
public int getInt(String key){
return Integer.parseInt(table.getProperty(key));
}
public String getString(String key){
return table.getProperty(key);
}
public double getDouble(String key){
return Double.parseDouble(table.getProperty(key));
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询